Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKMapView Route/Directions

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.

like image 941
Mark Avatar asked Sep 29 '09 20:09

Mark


People also ask

How do I show route in Apple Maps using Swift?

Create the function. Create a new Swift file from Xcode and call it OpenMapDirections, then copy/paste the following code in it: We will use the above code to present an alert controller with 2 actions: one for opening Google Maps, the other to open Apple Maps. Here we pass the coordinate for both directly.

What is MapKit in iOS?

MapKit is a powerful API available on iOS devices that makes it easy to display maps, mark locations, enhance with custom data and even draw routes or other shapes on top.


2 Answers

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.

like image 140
coffeemate Avatar answered Sep 19 '22 20:09

coffeemate


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.

like image 28
Ramin Avatar answered Sep 17 '22 20:09

Ramin