0

I am using Google Map SDK. I want to catch every moment when user changes his location, maybe from CLLocationManagerDelegate. Can anyone say which method can execute previous job?

I tried to use func locationManager( _ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) but it doesnot work well. When I run application, after 3-4 seconds this method call then If I change my location it doesnot do nothing.

Rezo Joglidze
  • 321
  • 1
  • 3
  • 15

1 Answers1

0

Did you call startUpdatingLocation()?

Suppose that the below code is your CLLocationManager.

    lazy var locationMgt: CLLocationManager = {
        let manager = CLLocationManager()
        manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
        manager.distanceFilter = 1 // 1 meter
        
        manager.activityType = .automotiveNavigation
        manager.allowsBackgroundLocationUpdates = true
#if os(iOS)
        manager.pausesLocationUpdatesAutomatically = false
#endif
        manager.delegate = self
        return manager
    }()

Then ask for permission and start updating the location:

locationMgt.requestWhenInUseAuthorization()

// you can call startUpdatingLocation() inside this function or call it right after requestWhenInUseAuthorization()
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    locationMgt.startUpdatingLocation()
}

Then your delegate implementation:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let last = locations.last else { return }
    // here user's last location is available.
}
Neklas
  • 504
  • 1
  • 9