Search code examples
iosobjective-cgoogle-maps-sdk-iosgoogle-polyline

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


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.

enter image description here

enter image description here


Solution

  • 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.