【问题标题】:polyline on the google map in realtime not working谷歌地图上的折线实时不起作用
【发布时间】:2016-11-01 08:00:55
【问题描述】:

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

我刚刚发现“Jumpy”当前位置不仅发生在我的应用程序中。它也发生在 GoogleMap 应用程序中。所以这可能是 Iphone/ios GPS 问题。

【问题讨论】:

  • 你只需要从位置数据中去除噪音。基本上,如果新值只是它的差异只有 0.0001(类似),则忽略新的位置值
  • 其实这个面积并没有那么小。它足够大,足以让用户转向不同的街道以将它们作为噪音过滤掉。我正在使用 iPhone 5 bty。

标签: ios google-maps swift2 xcode7


【解决方案1】:

您只需要忽略无效的位置更新或准确性低的更新。

- (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
    }
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-02
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    相关资源
    最近更新 更多