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.

comgooglemaps:

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:

comgooglemaps://

In your Corona app, you can use this API call:

system.openURL("comgooglemaps://")

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:

gmaps-collage

1. Map Coordinates and Traffic View

A basic coordinate view can be accomplished as follows:

system.openURL("comgooglemaps://?center=40.765819,-73.975866&zoom=14&views=traffic")

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 &.

system.openURL("comgooglemaps://?center=40.765819,-73.975866&zoom=14&views=traffic&mapmode=streetview")

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:

system.openURL("comgooglemaps://?q=pizza&center=40.765819,-73.975866")

4. Driving Directions

How about driving directions from point A to point B?

system.openURL("comgooglemaps://?saddr=Google+Inc,+8th+Avenue,+New+York,+NY&daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York&directionsmode=transit")

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.

Convenience Function

Since this URL parameter appending is somewhat tedious, you can use this clever function which will encode the URLs for you!

function urlencode(str)
   if (str) then
      str = string.gsub (str, "\n", "\r\n")
      str = string.gsub (str, "([^%w ])",
         function (c) return string.format ("%%%02X", string.byte(c)) end)
      str = string.gsub (str, " ", "+")
   end
   return str    
end

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?

url-scheme-errorOne 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.

local didOpenGoogleMaps =
      system.openURL("comgooglemaps://?daddr=San+Francisco,+CA&saddr=cupertino")

if ( didOpenGoogleMaps == false ) then  --defer to Apple Maps
      system.openURL("http://maps.apple.com/?daddr=San+Francisco,+CA&saddr=cupertino")
end

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.

    • Brent Sorrentino says:

      Hi Dave,
      You can still use URL schemes with Apple Maps (see the link provided near the end of the tutorial) and they work similarly. But now we have a choice which service to use, and a new “silent fail detection” to revert to Apple Maps if the Google Maps app isn’t installed. As for the old version of Google Maps, I don’t know if this works or not, but URL schemes are pretty standard and I assume they do work, at least at the basic level if not the full advanced level.

  1. I know its a off topic but how about a blog post about the upcoming photon 3.2 SDK supporting CoronaSDK

    From their newsletter:

    In the Making: Photon v3.2 and New SDKs
    A new and shiny Photon version 3.2 is currently in the making and with it some great new SDKs. .

    All new Corona SDK: Corona is a fantastic development framework and we are glad to hook into it with a dedicated SDK.

  2. I am working on an idea right now where I want the users of the app to all be able to collaborate on a single google map. Is there anyway to have the google api bring up a public collaborative map?

    Thanks,

    Josh

    • This should work on Android because Google Maps is the default mapping app. You may need one of the later daily builds to check the URL to make sure it’s launchable. At the time this was written, the test to see if system.openURL() was able to open the URL was iOS only, but now that works on Android.

      Corona SDK’s native mapView API call uses Google on Android devices where it uses Apple Maps on iOS.

    • In general, URL scheme’s like this are an iOS feature. This was a work around for Apple Maps, and really wasn’t that important for Android. Android does support a way of launching apps, but you have the native Google maps app already.

  3. As a newbie to Corona, I’m not fully certain where to drop these code lines in main.lua to test out the feature. Thus far, I’ve not gotten any basic Google Maps to appear. Could someone drop a short but *complete* main.lua sample code to demonstrate how this feature should be implemented? Thanks in advance!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>