简体   繁体   English

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

[英]Issue getting floor information using CLLocation and Google Map

I'm using the CLLocation for getting the current floor of a mall.我正在使用 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)")
    }
}

Now, when I run this code, its working fine until I load the google map.现在,当我运行这段代码时,它工作正常,直到我加载谷歌地图。 Here is the code.这是代码。

@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)
}

As soon as I load the Google Map, I started getting the floor from the CLLocation as Nil.一旦我加载了谷歌地图,我就开始从 CLLocation 获取地板为 Nil。 And following line of code is start to execute.下面这行代码开始执行。

floorLabel.text = "No floor detected"

Does anyone know whats going wrong with it?有谁知道它出了什么问题?

Due to the documentation you should first of all turn Indoor Maps on:由于文档,您应该首先打开Indoor Maps

mapView.isIndoorEnabled = true

Also the documentation says : 文档还说

Floor plans are available in select locations .平面图可在特定地点使用 If floor plan data is not available for a building that you would like to highlight in your application, you can: 1) Add floor plans to Google Maps directly.如果您想在应用程序中突出显示的建筑物的平面图数据不可用,您可以: 1) 直接将平面图添加到 Google 地图。 This will make your plans are available to all users of Google Maps.这将使您的计划可供 Google 地图的所有用户使用。 2) Display a floor plan as a Ground Overlay. 2) 将平面图显示为地面叠加层。 This will enable only users of your application to view your floor plans.这将仅允许您的应用程序用户查看您的平面图。

Also consider that还要考虑

By default, no location data is shown on the map.默认情况下,地图上不显示位置数据。 You may enable the blue "My Location" dot and compass direction by setting myLocationEnabled on GMSMapView您可以通过在 GMSMapView 上设置 myLocationEnabled 来启用蓝色的“我的位置”点和指南针方向

mapView.isMyLocationEnabled = true

More details on that.更多细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM