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

我正在使用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)")
    }
}

现在,当我运行此代码时,在加载Google地图之前,它可以正常工作。这是代码。

@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 Map,我就开始从CLLocation获取Nil。接下来的代码行开始执行。

floorLabel.text = "No floor detected"

有人知道它出了什么问题吗?

longxin0118 回答:使用CLLocation和Google Map获取楼层信息的问题

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

mapView.isIndoorEnabled = true

还有documentation says

  

select locations中提供了楼层平面图。如果您想在应用程序中突出显示建筑物的平面图数据不可用,则可以:1)将平面图直接添加到Google地图。这将使您的计划可供所有Google Maps用户使用。   2)显示一个平面图作为地面覆盖物。这样一来,只有您应用程序的用户才能查看您的平面图。

还要考虑

  

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

mapView.isMyLocationEnabled = true

更多details

本文链接:https://www.f2er.com/3037942.html

大家都在问