简体   繁体   中英

Issue getting floor information using CLLocation and Google Map

I'm using the CLLocation for getting the current floor of a mall.

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. 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:

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. This will make your plans are available to all users of Google Maps. 2) Display a floor plan as a Ground Overlay. 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

mapView.isMyLocationEnabled = true

More details on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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