6

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

ChenSmile
  • 3,401
  • 4
  • 39
  • 69
  • I would suggest logging all the co-ordinates of your drive route and try plotting them to see whether you get the same over lapped polyline, if so then your code is right. – Satheesh Nov 18 '16 at 07:21
  • @satheeshwaran could u answer it – ChenSmile Nov 18 '16 at 07:22
  • I don't see anything fishy in the code, you need to check the co-ordinates you are using to draw the line. See whether they follow the same pattern. – Satheesh Nov 18 '16 at 07:31

2 Answers2

0

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.

  • i tried earlier all this but it does not work. getting same thing when app goes in background – ChenSmile Nov 19 '16 at 13:01
  • do you use a GPX file to test ? – Zouhair ISKSIOU Nov 19 '16 at 14:30
  • No I want to track my location while walking y I need gpx – ChenSmile Nov 19 '16 at 14:33
  • it may be something wrong with GPS in your phone , try GPX if it works so check another phone , i used this to test your code and it work even in the background [link](http://www.bikely.com/maps/bike-path/copy-of-copy-of-frankfurt-berlin-hamburg-amsterdam-brussel-paris-luxemburg-munchen-praha-frankfurt) – Zouhair ISKSIOU Nov 19 '16 at 14:38
  • Could u send sample working code then I have tested in iPhone 5SE and 6S in both it s not working – ChenSmile Nov 19 '16 at 14:40
  • i just Copied/Past your code in empty project and added configure plist.info – Zouhair ISKSIOU Nov 19 '16 at 14:46
  • That's y I asked just send ur working code how could it it possible it s working in ur device and I tested in new device with latest OS and it s not working if possible send ur working code – ChenSmile Nov 19 '16 at 14:47
  • [Project zip](https://drive.google.com/file/d/0B3ShH098v1xqemxZVlFheF8xOFE/view?usp=sharing) – Zouhair ISKSIOU Nov 19 '16 at 14:47
  • i will check and get back to u. i am right now away from system – ChenSmile Nov 19 '16 at 14:54
  • i check with ur code and it works but when i start camera position automatically animate to near by position so polylines are drawn in triangle shape, it works till i walk/drive once stop map camera animate automatically. could u update on that to me, it will good to accept ur answer – ChenSmile Nov 21 '16 at 10:29
  • i didn't get it , please can you clarify more ? – Zouhair ISKSIOU Nov 21 '16 at 13:47
  • Hi all works good but I did not move from current position and zoom the map then cursor in google map automatically moves and draw poly lines to moved position ,so it looks like zigzag in current position is it possible to set region wise and cursor radius on zoom so that it should not zigzag if I zoom in and zoom out.hope u got my point else I can attach image to clarify more – ChenSmile Nov 21 '16 at 14:35
  • please look at small images and its triangle polylines in same posiiton – ChenSmile Nov 21 '16 at 18:57
  • I just edited my question and added a image plz look into it – ChenSmile Nov 21 '16 at 19:03
  • you have help me to figure out the problem if u can update my problem plz update i will accept ur answer – ChenSmile Nov 22 '16 at 05:20
  • Hello , i suggest to stop location updates when you stop , and starting it when the device detect mouvement , [Detecting if a user is moving in a car](http://stackoverflow.com/questions/17869659/detecting-if-a-user-is-moving-in-a-car) – Zouhair ISKSIOU Nov 22 '16 at 13:02
  • @ zouhair isk could u tell me how could i know user is in same location and then detch movement in location change could u update in code – ChenSmile Nov 23 '16 at 15:46
  • [this](http://stackoverflow.com/questions/17869659/detecting-if-a-user-is-moving-in-a-car) – Zouhair ISKSIOU Nov 27 '16 at 22:02
0

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.

fishinear
  • 6,101
  • 3
  • 36
  • 84
  • i will check ur suggestion and revert back to u – ChenSmile Nov 22 '16 at 03:06
  • your code does not work and make it crash if i idle. could us end working sample – ChenSmile Nov 22 '16 at 05:19
  • Luckily, a crash is easy to debug, so I am sure you'll be able to find where the fault is. As far as I know, the code I showed is working. – fishinear Nov 22 '16 at 08:56
  • @ishinear your code is not at all good to debug and it make no sense because theLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; theLocationManager.distanceFilter = kCLDistanceFilterNone; theLocationManager.activityType = CLActivityTypeOtherNavigation; when used cursor stop moving if device get moved, please first make it work on ur end then try to post answer. If code is working in ur end please send sample. Because i have tried many things and it is not working so i asked in bounty – ChenSmile Nov 22 '16 at 09:01
  • @Imran, I am just trying to help you out, no need to get upset. The code runs fine for me. If you have trouble putting it into your program, then please show what you have done, so I might be able to help you more. – fishinear Nov 22 '16 at 09:41
  • it does not work, i changed it in my code, as u r copying existing code lines up and down, i tasted and checked so i replied you, thanks for answering. If possible send working sample i will check and let u know – ChenSmile Nov 22 '16 at 09:43