How to use Google Maps data within your mobile application

Note: You can find this article also on Forum Nokia Wiki: How to use Google Maps data in mobile applications

Today we’ll see how to use Google Maps data within a mobile application.
Google Maps offers REST services that allows accessing its data with simple HTTP requests, so we can easily integrate them within our mobile apps.

Signup for a Google Maps API key

First thing you must do is to signup on this page:
http://code.google.com/apis/maps/signup.html
Once done, you’ll get a key (a simple String) you’ll use for all your query to Google Maps services

Static maps

Standard Google Maps code is suited for web applications, since it includes alot of Ajax functionalities, that are not really useful if you’re building a mobile application. So, the solution is to use static maps service, that will allow us to retrieve single images, easily usable within our apps.

Static maps service supports different image formats (png32, gif, jpg) and customizable image size, so that we can get perfect images for all our needs. As an example, suppose we want to retrieve the location at:

  • latitude: 41.867878
  • longitude: 12.471516

We can simply retrieve this URL with an HTTP GET request:

http://maps.google.com/staticmap?center=41.867878,12.471516&
format=png32&zoom=8&size=240x320&key=<API_KEY>

This way, we’ll get a PNG32 image, with a width of 240 pixels, and a height of 320, centered at point (41.867878,12.471516), and with a zoom level of 8 (zoom can go from 0 to a maximum level of 19).

Google Maps static image sample

Geocode an address

From Google Maps docs:
Geocoding is the process of converting addresses (like “1600 Amphitheatre Parkway, Mountain View, CA”) into geographic coordinates (like latitude 37.423021 and longitude -122.083739)

So, let’s assume we want to build an application that displays the address typed by our user. We should firstly geocode its address to geographics coordinates.
To do this, Google Maps offer another REST service easily accessible with simple HTTP requests.

Let’s say you want to geocode this address “Leicester Square, London”, then you’ll request this URL:

http://maps.google.com/maps/geo?q=Leicester%20Square,%20London
&output=csv&key=<API_KEY>

and you’ll get this output:

200,6,51.510605,-0.130728

Where:

  • the first number is a code, that in this case (200) means that geocoding has been successfull (for a full list of status codes you can see here: [1])
  • the second number gives a measure of geocoding accuracy (from 0 to 9 - maximum accuracy)
  • 3rd and 4th numbers represent latitude and longitude of the geocoded address, so these are the coordinate we’ll use to retrieve the map through the static map service we’ve seen before

As you can see, there is an ‘output’ parameter within the geocode request, and this means that we can choose the output format we prefer for our needs. Supported formats are:

  • xml
  • kml (same as xml, but with different Content-Type)
  • json (not really useful for mobile apps)
  • csv (comma separated values)

Proxy server, usage limits

Since your Google Maps API key is bound to a specific URL, to access map services you should setup a proxy server that will receive HTTP requests from your mobile application and forward them to Google Maps REST URLs, giving back Google responses to mobile clients.

Also, be aware that there is a limit to the number of requests (both for static maps and geocode service) you can do each day. For personal uses they’re more than enough, anyway consider this point if you plan to develop commercial services.

Google Maps J2ME API and sample application

Now, you want code right? :) Here it is:

Google Maps sample application screenshot

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • BlinkList
  • Furl
  • Ma.gnolia
  • MisterWong
  • NewsVine
  • Reddit
  • StumbleUpon
  • Technorati

Tags: , , ,

5 Responses to “How to use Google Maps data within your mobile application”

  1. Francesco Says:

    Hi!
    I’m trying to execute the code you posted on the Forum Nokia Wiki. I use the Sun Wireless Toolkit and the EsclipseME IDE.
    I get the following exception when invoking the retrieveStaticImage method:

    java.lang.IllegalArgumentException:
    at javax.microedition.lcdui.ImmutableImage.(+11)
    at javax.microedition.lcdui.Image.createImage(+40)
    at GoogleMaps.retrieveStaticImage(+28)

    could you help me?

    thanks a lot,
    francesco

  2. pit Says:

    Hi Francesco,

    this has probably something to do with illegal/corrupt data coming from server. If you try getting the map URL directly in your browser, does it work?

    Also, which format are you using for your static maps?

    Pit

  3. Francesco Says:

    Hi Pit,
    thanks for your fast answer. I tried to change the format for the static map to png16 (I was using png32) and it works :D

    Now I have a problem with displaying the image: I can see just the first few pixels while the rest of the screen is black (width is alright, while the problem is with the height)

    My code is pretty simple:

    Image map = gMap.retrieveStaticImage(320, 240, lanLng[0], lanLng[1], 15, “png16″);
    Form f = new Form(”Image”);
    f.append(map);
    display.setCurrent(f);

    It is my first approach to midlets and I don’t really know what’s wrong. I also trying using canvas but I get the same result. Do you have any suggestion?

    thanks again,
    francesco

  4. pit Says:

    Hi Francesco,

    sorry for the delay of my answer, but I totally missed your reply… :-/

    Anyway, do you have this problem on emulators, or on real devices?

    Pit

  5. Olaseni Says:

    Hi,
    Please I need help in finding a source code for designing a map on mobile application, I intend using a scanned image for the map.

Leave a Reply