views:

2806

answers:

2

I found that the Google Maps API supports Directions through:

var map;
var directionsPanel;
var directions;

function initialize() {
  map = new GMap2(document.getElementById("map_canvas"));
  directionsPanel = document.getElementById("my_textual_div");
  map.setCenter(new GLatLng(49.496675,-102.65625), 3);
  directions = new GDirections(map, directionsPanel);
  directions.load("from: 500 Memorial Drive, Cambridge, MA to: 4 Yawkey Way, Boston, MA 02215 (Fenway Park)");
}

So how can this be translated into Objective-C so it can be retrieved by the iPhone? I know how to draw a line on MKMapView I just need the geolocations for the route.

Or perhaps there is a different way to get the route between two geolocation points.

Please let me know,

Thanks in advance.

+3  A: 

I was originally going to say (and I'm sure others will bring it up) that the Google Maps terms-of-use says you can't use directions if you plan to use MapKit. But then I double-checked http://code.google.com/apis/maps/terms/iPhone.html and couldn't really find an explicit restriction. If I understand it correctly, it actually says you can't use directions as long as it's tied to a GPS-driven realtime turn-by-turn functionality. You should use your own judgement as to whether it's OK or not and whether it affects your chance of acceptance on the AppStore.

As far as getting the actual data in Objective-C your best best is to look at the Google Ajax search documentation [ http://code.google.com/apis/ajaxsearch/documentation/ ] especially the section under Flash and other Non-Javascript Environments. It explains a RESTful API where you can send HTTP GET requests to Google and get JSON data back. That's the only way I've found to get programmatic data out of Google that isn't tied to Javascript.

Once you've figured out the proper URL to invoke you can wrap the whole thing in an ASIHTTPRequest call and feed the result to a JSON parser. One thing to keep in mind is that the RESTful API returns only a few results (between 4 and 8) at a time and you have to keep going back until you've got all the data. There also appears to be a 64-item cap to the number of results returned by the API so some complex edge-cases may be affected.

Ramin
I think it's weird they support almost anything except directions. Oh well I'll guess I am better of using a forward to the actual google maps app.
Mark
+1  A: 

There is an undocumented Google Maps Directions API mentioned and exposed here

Also you should take a look at the sample IPhone application which draws routes/directions onto MKMapView using the API mentioned above here

But warning you about the licencing issues about the undocumented Google Maps Directions API.

Hope this helps.

coffeemate