[英]Catch every moment when user change his location
I am using Google Map SDK.我正在使用谷歌 Map SDK。 I want to catch every moment when user changes his location, maybe from CLLocationManagerDelegate.
我想捕捉用户更改位置的每一刻,可能来自 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.我尝试使用
func locationManager( _ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
但效果不佳。 When I run application, after 3-4 seconds this method call then If I change my location it doesnot do nothing.当我运行应用程序时,在 3-4 秒后调用此方法,然后如果我更改我的位置,它什么也不做。
Did you call startUpdatingLocation()
?您是否致电
startUpdatingLocation()
?
Suppose that the below code is your CLLocationManager
.假设以下代码是您的
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.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.