我正在尝试实时绘制多段线以向用户显示他们到目前为止所走的路线。我使用 google map api,到目前为止,它可以毫无问题地显示用户的当前位置。但是折线不起作用(它根本不绘制折线)。在检查授权并在 didupdatelocations 内绘制折线后,我调用 startMonitoringSignificantLocationChanges。这是我的代码的相关部分:
extension MapViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedAlways {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
locationManager.startMonitoringSignificantLocationChanges()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = manager.location {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
path.addCoordinate(CLLocationCoordinate2D(latitude: location.coordinate.latitude,
longitude: location.coordinate.longitude))
let polyline = GMSPolyline(path: path)
polyline.strokeColor = UIColor.redColor()
polyline.strokeWidth = 3
polyline.map = mapView
locationManager.stopUpdatingLocation()
}
}
UPDATE-1
在注释掉 stopUpdatingLocation 行之后,它确实绘制了这条线。但是这条线是乱七八糟的。
UPDATE-2
我想通了为什么这条线是一团糟而不是一条直线。这是因为 iphone 一直在改变当前位置(即使它是静止的),因此,在一个小区域绘制多条线。如何阻止 iphone 这样做?
UPDATE-3
我刚刚发现“跳跃”的当前位置不仅仅发生在我的应用程序中。它也发生在 GoogleMap 应用程序中。所以这可能是 Iphone/ios GPS 问题。
最佳答案
您只需要忽略无效的位置更新或精度低的更新。
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
NSTimeInterval age = -[location.timestamp timeIntervalSinceNow];
if (age > 120) return; // ignore old (cached) updates
if (location.horizontalAccuracy < 0) return; // ignore invalid updates
if (location.horizontalAccuracy <=10) //you can change 10 to 20 if you want more frequent updates
{
// this is a valid update
}
}
希望这对您有所帮助。
关于ios - 谷歌地图上的折线实时无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38102519/