Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps iOS SDK, Getting Directions between 2 locations

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, for which it is necessary that Google Maps App is installed on your device.

2.) Using Directions API, via Request-Response and then parsing the JSON. Displaying markers to show the direction.

Now, my question is there any other way by which I can do this on iOS? I need to show the direction from my current location to a particular location of which i have the Lat/Long.

I mean is it really not possible to simply pass 2 location as parameter and Google Maps SDK, will give me the directions?

Thanks,

like image 811
Amit Singh Avatar asked Jan 03 '14 14:01

Amit Singh


People also ask

Which system can be used to know route between two points?

Drawing Route Between Two Points Using Google Map.


2 Answers

    NSString *urlString = [NSString stringWithFormat:                        @"%@?origin=%f,%f&destination=%f,%f&sensor=true&key=%@",                        @"https://maps.googleapis.com/maps/api/directions/json",                        mapView.myLocation.coordinate.latitude,                        mapView.myLocation.coordinate.longitude,                        destLatitude,                        destLongitude,                        @"Your Google Api Key String"]; NSURL *directionsURL = [NSURL URLWithString:urlString];   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:directionsURL]; [request startSynchronous]; NSError *error = [request error]; if (!error) {     NSString *response = [request responseString];     NSLog(@"%@",response);     NSDictionary *json =[NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingMutableContainers error:&error];     GMSPath *path =[GMSPath pathFromEncodedPath:json[@"routes"][0][@"overview_polyline"][@"points"]];     GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path];     singleLine.strokeWidth = 7;     singleLine.strokeColor = [UIColor greenColor];     singleLine.map = self.mapView; } else NSLog(@"%@",[request error]); 

Note: make Sure Your Google Direction API Sdk Is Enable in Your google developer Console.

like image 113
Muhammad Noman Avatar answered Oct 05 '22 12:10

Muhammad Noman


It sounds like you are looking for UI Chrome like the Google Maps app has for showing directions. Google Maps SDK for iOS will paint you a map, but you are responsible for the additional navigation chrome.

You can use the Google Directions API to request directions, and then use the encoded path returned from the service to draw a GMSPolyline using GMSPath's pathFromEncodedPath: method.

like image 20
Brett Avatar answered Oct 05 '22 13:10

Brett