我尝试使用GoogleMap SDK记录用户正在使用的路径。我正在使用折线在地图上绘制线条以向用户显示所采用的路径。为了减少生成的坐标并使线看起来干净(而不是看起来波浪形),我正在调用CLLocationManger的startMonitoringSignificantChange而不是startUpdatingLocation。但是,这似乎不起作用。它在加载视图时只调用一次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])
}
}
}
}
答案 0 :(得分:0)
在locationManager.requestAlwaysAuthorization()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
P.S。如果您的视图持有locationManager
实例,则在卸载视图后,它可能无法接收位置更新。您可以删除以下行,因为它对于重要位置更新无效:
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters