0
我试图记录用户使用GoogleMap SDK的路径。我正在使用多段线在地图上绘制线条以向用户显示拍摄的路径。在尝试减少生成的坐标并使行看起来干净(而不是看起来很花哨)时,我调用CLLocationManger的startMonitoringSignificantChange而不是startUpdatingLocation。但是,这似乎并不奏效。它在加载视图时只调用一次didUpdateLocations方法,但在此之后,它只会停止调用。我在哪里做错了?startMonitoringSignificantLocationChange不会调用didUpdateLocations
这是我的代码后locationManager.requestAlwaysAuthorization()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
附:以下代码行
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])
}
}
}
}
当您期待更新而未获得更新时,您移动了多远? – dan
去5公里以外的市中心。 – user30646
您应该期望每隔500米更新一次。你是否将应用保持在前台并且设备保持清醒状态?您尚未启用后台位置信息,因此,如果您的应用已被暂停或设备处于睡眠状态,您将无法获得更新。 – Paulw11