I had a similar problem, I needed the direction path to be drawn on a Google map using the same google ios sdk maps. I made access to the Google Distance API .
I extracted html_instructions and end_location from the API using the following code.
I had these variables defined as a property
@property NSMutableArray *detailedSteps; @property (strong, nonatomic) IBOutlet GMSMapView *mapview;
And the following code to extract end_location and 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 showDirection method, which draws a polygon on a map. Here I pass the latlong array. This is actually an array of end_location.
-(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 ; }
Aadil keshwani
source share