Google map SDK: draw the correct polyline on the Google map as the device moves in the background

I am using the Google Map SDK for iOS. I draw polylines in driving mode.

But when I stop and scale the google map, my current position cursor automatically moves and redraws the zigzag polygons, because of which all previous polylines are drawn, overlapped, and the polylines are completely changed. The same thing happens when I go in the background and drive.

May I find out why this is happening? And how can I draw smooth polylines in driving and walking mode at the same time on the same path.

My code is

- (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 stay in the same position, then a Googme map. The cursor position automatically moves and draws such polylines.

enter image description here

enter image description here

+6
source share
2 answers

add in Info.plist NSLocationAlwaysUsageDescription and UIBackgroundModes → "location"

and

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

Before enabling background recreation: enter an image description here

After resolving upstream background objects: enter an image description here

Most of this route was done in the background.

0
source

Two things go on. Firstly, the GPS chip does not always return the same place when stopped. A specific GPS location always fluctuates a bit. iOS is making an effort to find that you are standing still and then delivering the same place, but I think this is done to a lesser extent in driving mode.

Secondly, using the convoluted way of storing samples as strings, you go through the %f conversion, which loses accuracy. This can exaggerate any differences between locations. If you use CLLocation objects directly, you are likely to get 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 in the CLLocationManager:

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

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

 self.mapContainerView = mapView_; 

You should simply use setNeedsDisplay in an existing view after updating the path.

0
source

Source: https://habr.com/ru/post/1258519/


All Articles