简体   繁体   中英

Remove travelled GMSPolyline segment

I am using google map sdk for ios to provide directions between current user location and an end location. I have so far achieved to draw a GMSPolyline between the current user location and the end location using the code below and it's working great.

GMSPath *encodedPath = [GMSPath pathFromEncodedPath:encodedPathSting];
self.polyline = [GMSPolyline polylineWithPath:encodedPath];
self.polyline.strokeWidth = 4;
self.polyline.strokeColor = [UIColor colorWithRed:55.0/255.0 green:160.0/255.0 blue:250.0/255.0 alpha:1.0];;
self.polyline.map = self.mapView;

Is it possible to remove a part of the GMSPolyline that has been covered by the user through driving/walking? The GMSPolyline must gradually decrease in length as we trace the path.

One way to achieve this is by redrawing the path repeatedly but this is not or may not be efficient.

Thanks.

So get the latlng point of the polyline in an array as described here :

 //route is the MKRoute in this example //but the polyline can be any MKPolyline NSUInteger pointCount = route.polyline.pointCount; //allocate a C array to hold this many points/coordinates... CLLocationCoordinate2D * routeCoordinates = malloc(pointCount * sizeof(CLLocationCoordinate2D)); //get the coordinates (all of them)... [route.polyline getCoordinates: routeCoordinates range: NSMakeRange(0, pointCount) ]; //this part just shows how to use the results... NSLog(@"route pointCount = %d", pointCount); for (int c = 0; c < pointCount; c++) { NSLog(@"routeCoordinates[%d] = %f, %f", c, routeCoordinates[c].latitude, routeCoordinates[c].longitude); } //free the memory used by the C array when done with it... free(routeCoordinates); 

Then, implement a while loop for the first point as you move along the path like this:

 int c = 0; while (pointCount.size() > 0) { pointCount.get(0).remove(); } 

Note: I'm not that experienced with iOS and haven't tested this solution. Treat it as a suggestion rather than a fix. Thanks!

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM