Question

I've been reading the documentation of the Google Maps SDK for iOS, and I didn't see anything about drawing routes on the map.

I know that Polylines are available, but I don't think that's the best aproach.

Is it possible?

Was it helpful?

Solution 3

There are some things in the library which aren't in the documentation, so if you're looking for a feature it's worth downloading the SDK and having a look at the headers.

However in the current version 1.0.2 I don't see anything for routing - either searching for a route or drawing them (or even looking up addresses). For now your only option is probably to use some other Google API to find the route, and then as Lee said, use polylines to draw them.

OTHER TIPS

The iOS SDK does not yet support directions. What you can do in the meantime is use the Directions API Web services and render the route yourself with a GMSPolyline.

You should also file a feature request to have this added to the SDK.

As Saxon Druce's answer, you can use the Google Maps Directions web service to find the route and draw them by polylines. This episode shows you how to realize it, and you can download the example code from this github repository.

I had similar problem i needed to have direction path to be drawn on google map using same google maps ios sdk. What i did was i accessed Google Distance API.

I extracted the html_instructions & end_location from API using following code.

I had these variables defined as property

@property NSMutableArray *detailedSteps;
@property (strong, nonatomic) IBOutlet GMSMapView *mapview;

And Following code to extract the end_location & html_instructions

NSURL *url=[[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=ahmedabad&destination=%@",self.city]];
NSURLResponse *res;
NSError *err;
NSData *data=[NSURLConnection sendSynchronousRequest:[[NSURLRequest alloc] initWithURL:url] returningResponse:&res error:&err];
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray *routes=dic[@"routes"];
 NSArray *legs=routes[0][@"legs"];
NSArray *steps=legs[0][@"steps"];
NSMutableArray *textsteps=[[NSMutableArray alloc] init];
NSMutableArray *latlong=[[NSMutableArray alloc]init];
for(int i=0; i< [steps count]; i++){
    NSString *html=steps[i][@"html_instructions"];
    [latlong addObject:steps[i][@"end_location"]];
    [textsteps addObject:html];
}
self.detailedSteps=textsteps;
[self showDirection:latlong];

And here is my method showDirection which draws polygon on the map. Here i am passing the latlong array. Which is actually end_location array.

-(void)showDirection:(NSMutableArray*) latlong{
GMSMutablePath *path = [GMSMutablePath path];
for(int i=0; i<[latlong count]; i++){
    double lat=[latlong[i][@"lat"] doubleValue];
    double lng=[latlong[i][@"lng"] doubleValue];
    [path addLatitude:lat longitude:lng];
}
NSLog(@"Direction path");
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeColor = [UIColor blueColor];
polyline.strokeWidth = 5.f;
polyline.map = self.mapview ;

}

Even version 1.1.0 doesn´t implement any tools for directions so i would recommend to use URL schemes for the best performance.

I think now it is possible to draw routes / direction using Google Maps iOS SDK.
Follow official video tutorial here:
http://talkminer.com/viewtalk.jsp?videoid=AdV7bCWuDYg&q=&row=4&N=5#.Ui2H1mSAZTE

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top