While I am using Google Maps SDK, I am trying to get driving direction between two locations on iOS. I know we can do this using two methods:-
1.) Using URL Scheme,
I had done it as it also shows PINS DISTANCE AND DURATION on map with DIRECTION ROUTE. But dont forget to set your GOOGLE DIRECTION API TO ENABLED in your GOOGLE DEVELOPER CONSOLE
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
NSString *urlString =@"https://maps.googleapis.com/maps/api/directions/json";
NSDictionary *dictParameters = @{@"origin" : [NSString stringWithFormat:@"%@",_sourceAdd], @"destination" : [NSString stringWithFormat:@"%@",_destinationAdd], @"mode" : @"driving", @"key":@"AIzaSyD9cWTQkAxemELVXTNUCALOmzlDv5b9Dhg"};
[manager GET:urlString parameters:dictParameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
GMSPath *path =[GMSPath pathFromEncodedPath:responseObject[@"routes"][0][@"overview_polyline"][@"points"]];
NSDictionary *arr=responseObject[@"routes"][0][@"legs"];
NSMutableArray *loc=[[NSMutableArray alloc]init];
loc=[[arr valueForKey:@"start_location"]valueForKey:@"lat"];
_sourceloc.latitude=[loc[0] doubleValue];
loc=[[arr valueForKey:@"start_location"]valueForKey:@"lng"];
_sourceloc.longitude=[loc[0] doubleValue];
loc=[[arr valueForKey:@"end_location"]valueForKey:@"lat"];
_destinationloc.latitude=[loc[0] doubleValue];
loc=[[arr valueForKey:@"end_location"]valueForKey:@"lng"];
_destinationloc.longitude=[loc[0] doubleValue];
NSString *dis,*dur;
loc=[[arr valueForKey:@"distance"]valueForKey:@"text"];
dis=loc[0];
loc=[[arr valueForKey:@"duration"]valueForKey:@"text"];
dur=loc[0];
NSString *sa,*da;
loc=[arr valueForKey:@"start_address"];
sa=loc[0];
loc=[arr valueForKey:@"end_address"];
da=loc[0];
UIAlertView *av=[[UIAlertView alloc]initWithTitle:@"Route Info" message:[NSString stringWithFormat:@"Distance:%@ \nDuration:%@",dis,dur] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
[av show];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:_sourceloc.latitude longitude:_sourceloc.longitude zoom:10];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
GMSMarker *marker = [GMSMarker markerWithPosition:_sourceloc];
marker.title=@"Source";
marker.snippet =sa;
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = mapView;
GMSMarker *marker2 = [GMSMarker markerWithPosition:_destinationloc];
marker2.title=@"Destination";
marker2.snippet =da;
marker2.appearAnimation = kGMSMarkerAnimationPop;
marker2.map = mapView;
GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path];
singleLine.strokeWidth = 4;
singleLine.strokeColor = [UIColor blueColor];
singleLine.map = mapView;
self.view = mapView;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];