ios - 谷歌地图上的折线实时无法正常工作

标签 ios google-maps swift2 xcode7

我正在尝试实时绘制多段线以向用户显示他们到目前为止所走的路线。我使用 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/

相关文章:

ios - 防止 iphone 电子邮件客户端缩放响应电子邮件

iphone - 拖动时为 UIView 制作动画

google-maps - 如何使用 GMaps.js 的 fitBounds

javascript - 在javascript中查找中间纬度和经度

ios - 在键中嵌套 JSON 字典?

iphone - 暂时隐藏 UINavigationBar 按钮

java - Android Google Map API v2 - 膨胀类 fragment 时出错 android.view.InflateException

ios - did设置使用建议?

ios - UITableview 在弹出 View Controller 后丢失了引用

ios - 使用 nib ViewControllers 设置 UITabBarController