简体   繁体   English

捕捉用户更改位置的每一刻

[英]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.

相关问题 用户移动手机时更改输出扬声器 - change output speaker when user move his phone iOS-用户是否允许使用其当前位置 - iOS - if user allow using his current location or not 检查用户是否接受该应用知道他的位置 - Check if user accepted that the app know his location 在GMSMapView中更改用户位置 - Change user location in GMSMapView 如何捕捉MKMapView缩放级别的每个变化 - How to catch every change in the zoom level of MKMapView MKMapView:显示用户当前位置时,引脚会更改颜色 - MKMapView: Pins Color Change When User Current Location is Shown 如何每5秒钟或当用户移动阈值而不耗尽电池时如何获得高精度位置? - How can I get high accuracy location every 5 seconds or when user moves a threshold without draining the battery? 用户在其iOS应用程序中登录时访问基本配置文件 - Access basic profile when user login in his/her iOS app 当我运行我的应用程序时,它不会注册用户位置,但是当我在调试器中更改位置时,它会开始正常注册它 - when i run my app it doesn't register the user location but when I change the location in the debugger it starts registering it normally 用户允许位置跟踪时更新位置 - Update location when user allows location tracking
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM