Google地图SDK:在Google地图中以设备在后台移动时绘制正确的折线

我正在使用Google Map SDK for iOS。 我正在驾驶模式中绘制多段线。

但是,当我停下来,然后缩放谷歌地图,我的当前位置光标自动移动和重绘之字形多段线,由于所有以前的多段线绘制得到重叠和多段线得到完全改变。当我去后台和驱动器时发生的事情。

我能知道为什么会发生? 而我怎样才能在相同的path上同时在驾驶和行走模式中绘制平滑的多段线。

我的代码 –

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

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

在这里输入图像说明

在这里输入图像说明

添加一个NSLocationAlwaysUsageDescription和一个UIBackgroundModes – >“location”给Info.plist

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

在允许后台位置更新之前: 在这里input图像描述

在允许后台位置更新之后: 在这里input图像描述

这个行程大部分都是在后台画出来的。

有两件事正在发生。 首先,GPS芯片在静止时不总是返回相同的位置。 确定的GPS位置总是波动一点。 iOS会努力检测到你处于静止状态,然后提供相同的位置,但是我认为在驾驶模式下这是做得较less的。

其次,通过使用复杂的方式将样本存储为string,您将进行%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_; 

在更新path之后,您应该在现有视图上使用setNeedsDisplay。