Google Maps are back — and we can use them in our Corona iOS apps today!
The real 12-12-12 world shocker was Apple releasing the new Google Maps app for the iPhone to replace the not-so-well-received Apple Maps. Google now offers turn-by-turn directions, vector based map tiles and a revamped interface.
The URL Scheme
First, let’s discuss the origin of the URL scheme. Back in the 1990s when the World Wide Web first came to us, developers needed a way to reference things on the Internet, so they came up with a Uniform Resource Locator or URL for short. There are many things that you can access over the Internet, and it’s not just web pages or images. You can open a terminal session, connect to a Gopher service, download a file from an FTP server, send an e-mail message, and several other things. The bits of the URL before the colon is the URL scheme. Some of the common ones include:
- http: — a file from a web server
- https: — a file from an encrypted web server
- mailto: — send an email message
- ftp: — access a file via FTP
- telnet: — open terminal sessions to a server
With iOS, the magic of being able to touch a phone number in an e-mail to open the dialer and start a call is made possible by URL schemes such as:
- tel: — make a phone call
- sms: — send a text message
- itms-apps: — opens the app store
- music: — go to the current playing song in the music app
Apple lets applications define their own scheme so that other apps can open your app. Corona Labs introduced this feature a year ago. You can read the blog post here to learn the basics of setting up schemes for your app(s).
Google Maps URL Scheme
To give developers quick access to their Maps app, Google uses a URL scheme which allows your app to access it.
Of course, the URL scheme is just part of the URL. You also have the host name and the resource on the host. Since apps don’t really have host names and they typically don’t have access to individual files, the full URL is more like:
In your Corona app, you can use this API call:
Your app will suspend and the Google Maps app will start up. Pretty cool, huh? Actually it gets better! Wouldn’t you like to specify a location or find a pizzeria nearby? Or other things that you can do in the Google Maps app, but directly from your app?
By adding additional information to the URL scheme, you can accomplish very specific things in the application. Here are some examples:
1. Map Coordinates and Traffic View
A basic coordinate view can be accomplished as follows:
This will open the map and have it center on the Latitude/Longitude of 40.7N and 73.9W, zoomed in to level 14 and showing traffic — this example happens to be Central Park in New York City. If you look carefully, you’ll notice that the first parameter you pass to the URL scheme is preceded by a question mark (?), and each parameter beyond that is separated by an ampersand (&). So, if we split apart this scheme, it looks like this:
?center=40.765819,-73.975866 --center map at these coordinates &zoom=14 --zoom to level 14 &views=traffic --show traffic view
2. Map “Street View”
We can see the “street view” of a map by simply adding the mapmode=streetview parameter, separated from the previous parameter by an &.
3. Map Query
Want to find that hot, tasty pizza? For this, we add the parameter q=pizza and we place it before the other parameters:
4. Driving Directions
How about driving directions from point A to point B?
This one looks more complicated at first glance, but basically we’re just using the parameters:
?saddr=... --"s" for "starting address" &daddr=... --"d" for "destination address" &directionsmode=
What are all of those plus symbols for? In URLs, spaces are not “safe” characters to pass to services. You must either replace spaces with + symbols, or use the hex code %20 to represent spaces. If you need an actual plus symbol in the URL (such that it won’t be considered as a space), you have to use its hex code of %2B.
Since this URL parameter appending is somewhat tedious, you can use this clever function which will encode the URLs for you!
system.openURL("comgooglemaps://?saddr=" .. urlencode("Google Inc., 8th Avenue, New York, NY") .. "?daddr=" .. urlencode("John F. Kennedy International Airport, Van Wyck Expressway, Jamaica, NY") .. "?directionsmode=transit")
As you can see, we call the encoding function from within the openURL call in a clear, readable format.
Maps App Installed or Not Installed?
One potential problem with URL schemes is that if the user doesn’t have the target app installed on his/her device (in this case, Google Maps), how can a URL scheme from a Corona app open it? Obviously it can’t — because there’s no app to open! In the current Corona Public Release (#971), your end users might receive an error message pop-up or a “silent fail” when the target app isn’t installed. Obviously this is not ideal.
Fortunately starting with Corona Daily Build #986, we now have control over this in the form of a boolean true/false that is returned from the system.openURL() API call.
The value of our variable didOpenGoogleMaps (it can be named whatever you wish) will be true if the Google Maps app is installed on the device, false if it isn’t installed. This allows you, the Corona developer, to perform a specific action within your app if the target app doesn’t exist. Very convenient! Remember that this specific feature was implemented in Daily Build #986, so please download it now if you’re a Pro/Indie subscriber.
Ready to Begin?
As you can see, the implementation of Google Maps in Corona is simple using a URL scheme. If you want to explore all of the capabilities that Google Maps offers, please review the Google Maps URL Scheme website here. If you prefer to use Apple Maps or desire it as a backup option to Google Maps, you can review the documentation here. Further questions? Please post your comments and questions below as usual.
Posted by Rob Miracle. Thanks for reading...