【问题标题】:Google Map SDK: Draw correct polyline in google map as Device Moves in Background谷歌地图 SDK:当设备在后台移动时,在谷歌地图中绘制正确的折线
【发布时间】:2017-03-02 05:13:21
【问题描述】:

我正在使用适用于 iOS 的 Google Map SDK。我正在驾驶模式下绘制折线。

但是当我停下来,然后缩放谷歌地图时,我的当前位置光标会自动移动并重新绘制锯齿形折线,因为之前绘制的所有折线都会重叠并且折线会完全改变。当我进入后台并开车时也会发生同样的事情.

我能知道为什么会这样吗?以及如何在同一路径上同时在驾驶和步行模式下绘制平滑的折线。

我的代码-

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
 pointString=[NSString     stringWithFormat:@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude];
CLLocationDistance kilometers = [newLocation distanceFromLocation:oldLocation] / 1000;
NSLog(@"Distance Travelled in Kilometer :%f",kilometers);

[self.points addObject:pointString];
GMSMutablePath *path = [GMSMutablePath path];
for (int i=0; i<self.points.count; i++)
{
    NSArray *latlongArray = [[self.points   objectAtIndex:i]componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];

    [path addLatitude:[[latlongArray objectAtIndex:0] doubleValue] longitude:[[latlongArray objectAtIndex:1] doubleValue]];
}

if (self.points.count>2)
{
    GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
    polyline.strokeColor = [UIColor blueColor];
    polyline.strokeWidth = 5.f;
    polyline.map = mapView_;
    self.mapContainerView = mapView_;
}
}

如果,我保持在同一位置,那么谷歌地图光标位置会自动移动并像这样绘制折线。

【问题讨论】:

  • 我建议记录你的行车路线的所有坐标并尝试绘制它们,看看你是否得到相同的重叠折线,如果是,那么你的代码是正确的。
  • @satheeshwaran 你能回答吗
  • 我没有在代码中看到任何可疑之处,您需要检查用于绘制线条的坐标。看看他们是否遵循相同的模式。

标签: ios objective-c google-maps-sdk-ios google-polyline


【解决方案1】:

NSLocationAlwaysUsageDescriptionUIBackgroundModes -> "location" 添加到 Info.plist

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
    manager.allowsBackgroundLocationUpdates = YES;
}

在允许后台位置更新之前: enter image description here

允许后台位置更新后: enter image description here

此行程的大部分内容已在后台绘制。

【讨论】:

  • 我早些时候尝试了所有这些,但它不起作用。当应用进入后台时得到同样的结果
  • 你用GPX文件测试吗?
  • 不,我想在走路时跟踪我的位置,我需要 gpx
  • 这可能是您手机中的 GPS 有问题,尝试 GPX 如果它可以正常工作,请检查另一部手机,我用它来测试您的代码,它甚至可以在后台运行link
  • 您能否发送示例工作代码,然后我在 iPhone 5SE 和 6S 中都进行了测试,但都无法正常工作
【解决方案2】:

有两件事正在发生。首先,GPS 芯片在静止时并不总是返回相同的位置。确定的 GPS 位置总是有一点波动。 iOS 会努力检测您是否静止不动,然后提供相同的位置,但我认为在驾驶模式下这样做的程度较小。

其次,通过使用复杂的方式将样本存储为字符串,您会经历%f 转换,这会降低准确性。这可能会夸大位置之间的任何差异。如果您直接使用 CLLocation 对象,您可能会得到更好的结果(以及更简洁的代码):

[self.points addObject:newLocation];
GMSMutablePath *path = [GMSMutablePath path];

for (CLLocation *col in self.points)
{
    [path addLatitude:col.latitude longitude:col.longitude];
}

另外,请确保您在 CLLocationManager 上设置正确:

theLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
theLocationManager.distanceFilter = kCLDistanceFilterNone;
theLocationManager.activityType = CLActivityTypeOtherNavigation;
theLocationManager.allowsBackgroundLocationUpdates = YES

另一件事。在didUpdateToLocation: 方法中更改视图也很奇怪:

self.mapContainerView = mapView_;

更新路径后,您应该只在现有视图上使用 setNeedsDisplay。

【讨论】:

  • 我会检查你的建议并回复你
  • 您的代码不起作用,如果我空闲,它会崩溃。我们可以结束工作样本吗
  • 幸运的是,崩溃很容易调试,所以我相信您将能够找到故障所在。据我所知,我展示的代码是有效的。
  • @ishinear 您的代码根本不好调试,而且没有任何意义,因为 theLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; theLocationManager.distanceFilter = kCLDistanceFilterNone; theLocationManager.activityType = CLActivityTypeOtherNavigation;当使用的光标停止移动时,如果设备被移动,请先让它在你的一端工作,然后尝试发布答案。如果代码在您的最终工作,请发送示例。因为我尝试了很多东西但它不起作用所以我问了赏金
  • @Imran,我只是想帮你,没必要生气。代码对我来说运行良好。如果您无法将其放入您的程序中,请展示您所做的工作,以便我可以为您提供更多帮助。
猜你喜欢
  • 1970-01-01
  • 2013-05-30
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 2016-05-14
  • 1970-01-01
  • 2013-07-02
  • 2023-04-06
相关资源
最近更新 更多