Google Static Maps Android With Examples

In this tutorial we’re going to implement a google static maps in our android application. Google Static Maps API typically shows fewer points of interest compared to Google Maps.

Google Static Maps

  1. Google Static Maps API allow us to dynamically create a map image based on URL parameters (i.e. latitude, longitude, zoom level, encoded polyline etc.) sent through a standard HTTP request. When called, it returns the map as an image allowing developers to display it on the user interface.
  2. Google Static Maps API returns an image (either GIF, PNG or JPEG) in response to an HTTP request via a URL. For each request, we can specify the location of the map, the size of the image, the zoom level, the type of map, and the placement of optional markers at locations on the map. We can additionally label your markers using some standard alphanumeric characters.

A Google Static Map must be of the form:

The parameters of the url must be properly URL encoded before the http request is sent.

Google Static Maps parameters

Some required parameters for google static maps are described below.

  1. center: This defines the center of the map, equidistant from all edges of the map. This parameter takes a location as either a comma-separated {latitude, longitude} pair (e.g. “40.714728,-73.998672”) or a string address (e.g. “city hall, new york, ny”) identifying a unique location on the face of the earth.
  2. zoom: This defines the zoom level of the map, which determines the magnification level of the map. This parameter takes a numerical value corresponding to the zoom level of the region desired.
  3. size: This defines the rectangular dimensions of the map image. This parameter takes a string of the form {horizontal_value}x{vertical_value}. For example, 500×400 defines a map 500 pixels wide by 400 pixels high.

Several other parameters like markers and path take multiple locations. In those cases, the locations are separated by the pipe (|) character.

Google Static Maps Android Application

Let’s create an application that returns a google static map image and display it in a circular ImageView.

Below image shows the android studio project structure.

android-staticmaps-project

Google Static Maps Android Code

In this tutorial we’ve used a third party library to create a CircularImageView. We recommend using and integrating third party libraries wherever feasible as it saves time in writing some standard custom classes that are pretty common.

To send and receive the http url and response you need to add a few jar files in the project.

  • httpclient-4.3.3.jar
  • httpcore-4.3.3.jar
  • httpmime-4.3.3.jar

Sync the gradle dependencies to add the libraries in the project. On running this project now a DuplicateFileExpection might arise in the build.gradle. It’s due to conflicting package files of the libraries. A workaround is adding the following in the android tag of the build.gradle file.

The content_main.xml is defined as below.

The MainActivity.java looks like below.

In the above code we’ve url encoded the markers parameters and path parameters using URLEncoder.encode(path, "UTF-8");.

This makes the string url compatible by removing the white spaces and replacing them by %20C etc.

An AsyncTask anonymous inner class is created that sends the encoded static map api url using http call and the response returned is the Bitmap static map image of the url.
That bitmap is loaded into the CircularImageView. The following snippet is used to decode the Bitmap from the response stream.

In this example we’ve set two markers and a path consisting of some prominent landmarks across the globe. For many points along the path we can pass an encoded polyline string too. This will be discussed in a later tutorial.

The output of the application is given below:

android-staticmap-output

This brings an end to google static maps android application tutorial. You can download the final Android StaticMaps Project from the link below.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: