gpt4 book ai didi

ios - startMonitoringSignificantLocationChange 不会调用 didUpdateLocations

转载 作者:行者123 更新时间:2023-11-30 12:49:22 25 4
gpt4 key购买 nike

我尝试使用 GoogleMap SDK 记录用户正在走的路径。我正在使用折线在 map 上绘制线条,以向用户显示所采取的路径。为了减少生成的坐标并使线条看起来干净(而不是看起来弯曲),我调用 CLLocationManger 的 startMonitoringSignificantChange 而不是 startUpdatingLocation。然而,这似乎不起作用。当 View 加载时,它只调用一次 didUpdateLocations 方法,但之后,它就停止调用。我哪里做错了?

这是我的代码

    override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.requestAlwaysAuthorization()

locationManager.startMonitoringSignificantLocationChanges()

}

extension LocationViewController: CLLocationManagerDelegate {

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedAlways {
locationManager.startMonitoringSignificantLocationChanges()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

if let location = manager.location {

path.addCoordinate(CLLocationCoordinate2D(latitude: location.coordinate.latitude,
longitude: location.coordinate.longitude))
let polyline = GMSPolyline(path: path)
polyline.strokeColor = UIColor.redColor()
polyline.strokeWidth = 3
polyline.geodesic = true
polyline.map = mapView

// Save the coordinates to array
coordinateArray.append([location.coordinate.latitude, location.coordinate.longitude])



}


}

}
}

最佳答案

locationManager.requestAlwaysAuthorization()后添加以下代码行

locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false

附:如果您的 View 持有 locationManager 实例,则在卸载 View 后它可能不会收到位置更新。您可以删除以下行,因为它对于重要位置更新没有用:

locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters

关于ios - startMonitoringSignificantLocationChange 不会调用 didUpdateLocations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41190152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com