Wednesday, 27 November 2019

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?



from Issue getting floor information using CLLocation and Google Map

No comments:

Post a Comment