Google Map SDK: Draw correct polyline in google map as Device Moves in Background

喜你入骨 提交于 2019-11-30 08:37:18

问题


I am using Google Map SDK for iOS. I am drawing polylines in Driving mode.

But when i stop,and Zoom google map then, my Current position cursor automatically moves and redraw zigzag polylines, due to that all previous polylines drawn get overlapped and polylines get completely changed.Same things happens when i go in background and drive.

Could i know why is it happening? And How can I draw smooth polylines in driving and walking mode same time in same path.

My Code-

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

If , I remain in Same position, then Googme map Cursor position automaticalaly moves and draw polylines like this.


回答1:


add a NSLocationAlwaysUsageDescription and a UIBackgroundModes -> "location" to Info.plist

AND

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

Before allowing background location updtaes: enter image description here

After Allowing background location updtaes: enter image description here

Most of this itinerary has been drawn in the background.




回答2:


Two things are going on. First, the GPS chip does not always return the same location when standing still. The determined GPS location always fluctuates a bit. iOS does an effort to detect that you're standing still, and then supply the same location, but I think that is done to a lesser extend in Driving mode.

Second, by using the convoluted way to store the samples as strings, you go through a %f conversion, which looses accuracy. That can exaggerate any differences between locations. If you use the CLLocation objects directly, you're likely getting a better result (and much cleaner code):

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

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

Also, make sure you set the correct settings on the CLLocationManager:

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

One other thing. It is also very strange that you change the view in the didUpdateToLocation: method:

self.mapContainerView = mapView_;

You should just use setNeedsDisplay on the existing view, after updating the path.



来源:https://stackoverflow.com/questions/40150430/google-map-sdk-draw-correct-polyline-in-google-map-as-device-moves-in-backgroun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!