views:

2399

answers:

2

I am using UIMapView to display locations on the iPhone. I want to do a directions from current location to the location of interest, I don't think its possible using MapKit (but if it is please inform) So I will open either the Google Maps application or safari to display it.

Can i do this by specifying co-ordinates from (current location) to co-ordinates (the location of interest) I have these longitudes and latitudes. Or do i have to use street addresses?

If I do have to use street addresses, can i get them from the latitude and longitude.

+10  A: 

Yeah, it's not possible using MapKit. You could try to form a Google maps url request that contains both your current location and destination that will open in the Google maps app with the directions. Take a look at this wiki with info on formatting a google maps request url:

http://mapki.com/wiki/Google_Map_Parameters

Here's an example url:

http://maps.google.com/?saddr=34.052222,-118.243611&daddr=37.322778,-122.031944

Here's how you could implement this in your code:

CLLocationCoordinate2D start = { 34.052222, -118.243611 };
CLLocationCoordinate2D destination = { 37.322778, -122.031944 }; 

NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
         start.latitude, start.longitude, destination.latitude, destination.longitude];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
Adolfo
+1  A: 

A solid solution is to create a view controller with a NIB that includes a UIWebView, and then pass the URL that exercises Google's map / direction services. This way, you keep the user in the application. This approach is not sufficient when pulling up a web page, because the Apple kit doesn't support zooming. But with OS4, at least the user can double click the home button and switch back to the app.

Kirk

Kirk