使用 CLLocation 和 Google Map 获取楼层信息的问题

Posted

技术标签:

【中文标题】使用 CLLocation 和 Google Map 获取楼层信息的问题【英文标题】:Issue getting floor information using CLLocation and Google Map 【发布时间】:2019-11-25 08:54:30 【问题描述】:

我正在使用 CLLocation 来获取商场的当前楼层。

private var currentLocation = CLLocation() 
    didSet 
        locationLabel.text = "Longitude = \(currentLocation.coordinate.longitude) \nLatitude = \(currentLocation.coordinate.latitude)"
        if let level = currentLocation.floor?.level 
            floorLabel.text = "Floor = \(level)"
         else 
            floorLabel.text = "No floor detected"
        
    


extension ViewController: CLLocationManagerDelegate 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
        if let currentLocation = locations.first 
        self.currentLocation = currentLocation
        
    

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) 
        print("locationManager didFailWithError: \(error.localizedDescription)")
    

现在,当我运行此代码时,它工作正常,直到我加载谷歌地图。这是代码。

@IBAction func mapInitClicked(_ sender: Any) 
    let mapView = GMSMapView(frame: mapContainerView.bounds)
    mapView.autoresizesSubviews = true
    mapView.autoresizingMask = [.flexibleHeight, .flexibleWidth, .flexibleTopMargin, .flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin]
    mapView.settings.compassButton = true
    mapView.settings.indoorPicker = false
    mapView.isIndoorEnabled = false
    mapView.settings.myLocationButton = true
    mapView.isBuildingsEnabled = false

    //mapView.isMyLocationEnabled = true
    //floorLevel = mapView.indoorDisplay.activeLevel?.shortName
    if currentLocation.coordinate.latitude == 0.0
    
        let newCamera = GMSCameraPosition.camera(withLatitude: 40.7139018, longitude: -74.0156599, zoom: 19.5)
        mapView.camera = newCamera
    
    else
    
        let newCamera = GMSCameraPosition.camera(withLatitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude, zoom: 1.0)
        mapView.camera = newCamera
    
    mapContainerView.addSubview(mapView)

一加载 Google 地图,我就开始从 CLLocation 获取发言权为 Nil。下面的代码行开始执行。

floorLabel.text = "No floor detected"

有谁知道它出了什么问题?

【问题讨论】:

对这个社区没有一个评论/答案?很奇怪。 在哪里调用API来更新位置? @vadian,只要用户移动设备,位置就会自动更新。 那位置管理器好像没有检测到楼层。 不,它会在我加载 Google 地图之前检测到地板。一旦地图加载并显示在屏幕上,地板检测就会停止工作。 【参考方案1】:

由于documentation,您应该首先打开Indoor Maps

mapView.isIndoorEnabled = true

还有documentation says:

平面图可在select locations 获得。如果您想在应用程序中突出显示的建筑物的平面图数据不可用,您可以: 1) 将平面图直接添加到 Google 地图。这将使您的计划可供 Google 地图的所有用户使用。 2) 将平面图显示为地面叠加层。这将只允许您的应用程序的用户查看您的平面图。

同时考虑

默认情况下,地图上不显示位置数据。您可以通过在 GMSMapView 上设置 myLocationEnabled 来启用蓝色的“我的位置”点和指南针方向

mapView.isMyLocationEnabled = true

更多details。

【讨论】:

以上是关于使用 CLLocation 和 Google Map 获取楼层信息的问题的主要内容,如果未能解决你的问题,请参考以下文章

应用程序和谷歌地图中的距离之间的CLLocation差异

如何使用 iPhone 的 CLLocation 类将纬度和经度分配给变量?

[CLLocation] 和 CLLocation 的区别

CLLocation + Weather(Alamofire) 问题

CLLocation 如何实现 Equatable 协议?

iOS 使用 CLLocation 和 MapView 一起获取坐标和更新地图