views:

1173

answers:

3

Programming an iPhone App, I need to use Google Maps api to get directions and routes. Unfortunatelly, Google Maps works around javascript.

Any idea how to do this?

+1  A: 

You can implement this by using a UIWebView. When it comes to using javascript to access the Google Maps service through its Google Maps APIs, you simply take advantage of the UIWebView method

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script

Note that you will need to escape in the string representing your script, all of the occurrences of double quotes as needed.

As an example, the following method, needed to center a map, is taken from chapter 12 of iPhone SDK programming, by Maher Ali:

-(void) centerMap:(NSUInteger) index{
CLLocation *loc = [locations objectAtIndex:index];
NSString *js = 
[NSString stringWithFormat:
 @"var map = new GMap2(document.getElementById(\"map_canvas\"));"
 "map.setMapType(G_HYBRID_MAP);"
 "map.setCenter(new GLatLng(%lf, %lf), 18);"
 "map.panTo(map.getCenter());" 
 "map.openInfoWindow(map.getCenter(),"
 "document.createTextNode(\"Loc: (%i/%i), Time: %@\"));", 
 [loc coordinate].latitude, [loc coordinate].longitude,
 index+1, [locations count], 
 [loc timestamp]];
[webView stringByEvaluatingJavaScriptFromString:js];

}

Also, bear in mind the following. From the Apple documentation:

JavaScript execution time is limited to 10 seconds for each top-level entry point. If your script executes for more than 10 seconds, Safari stops executing the script. This is likely to occur at a random place in your code, so unintended consequences may result. This limit is imposed because JavaScript execution may cause the main thread to block, so when scripts are running, the user is not able to interact with the webpage.

JavaScript allocations are also limited to 10 MB. Safari raises an exception if you exceed this limit on the total memory allocation for JavaScript.

unforgiven
+1  A: 

Since you need to comply with OS 3.0 API to launch your app on the appstore you might as well get started using the MapKit API methods to embed a Google Map within your application without WebView. The above solution is how Google maps used to be 'embedded' within your app, but now you can use MapKit with OS 3.0.

Nael El Shawwa
+2  A: 

Before doing this you should be aware that it's against Google's Terms and Conditions to use Maps data in a Paid application.

From http://code.google.com/apis/maps/terms.html

9.1 Free, Public Accessibility to Your Maps API Implementation. Your Maps API Implementation must be generally accessible to users without charge. You may require users to log in to your Maps API Implementation if you do not require users to pay a fee. Unless you have entered into a separate written agreement with Google or obtained Google's written permission, your Maps API Implementation must not:

(a) require a fee-based subscription or other fee-based restricted access; or (b) operate only behind a firewall or only on an internal network (except during the development and testing phase).

10.8 use the Static Maps API other than in an implementation in a web browser;

I've included section 10.8 as well. This is a debatable one as you could argue that UIWebView is a browser but still be aware of it.

If you do decide to go ahead and use it the following is a really good implementation that bridges most of the code you'll need, http://code.google.com/p/iphone-google-maps-component/

NeilInglis