Setting up Google Maps on Android

Today I’m offering some tips on how to set up Google Maps in your Android application. This is not meant to be a start to finish tutorial on the process, but instead a few tips to move past some of the stumbling blocks I’ve run into.

Finding your app’s SHA-1 key

To set up a Google API key, you will need your app’s SHA-1 key. The easiest way to get both your debug and release (assuming you have a signing config setup for one of your build variants) SHA-1 keys is to use the method demonstrated here:

http://stackoverflow.com/a/34223470

in Android Studio.

Alternatively, the debug SHA-1 key can be found via command line by navigating to your ~/.android directory and running the following command (on Mac):

  • keytool -list -v -keystore debug.keystore
  • The password should be “android”.
  • Similarly, the release SHA-1 key can be found by running keytool -list -v -keystore YOUR_KEY_STORE_FILENAME.jks in whatever directory your keystore is located. The password will be the keystore password.

Getting a Google API key

  • Head to the Google API Manager console here: https://console.developers.google.com/apis/

  • Enable the Google Maps Android API

  • Go to the Credentials page and click Create credentials, choose API key.

  • When prompted with your new API key, click Restrict Key.

  • Name your key if you would like, then under the Key restriction section click “Android apps”

  • Click “Add package name and fingerprint” and enter your app’s package name (found in the android project’s AndroidManifest.xml) and the correct SHA-1 key. You will likely want at least two of these package name / fingerprint entries, one with the debug SHA-1 key and one with the release SHA-1 key. If you have build types that alter the package name, you will want to create additional package name / fingerprint entries for them. For example, say I append “.beta” to a build type that I upload to Crashlytics Beta. The package name “io.oakcity.project.beta” will need its own entry with a release SHA-1 key.

Setting Google API credentials in your app

Add the following meta-data tag to your AndroidManifest.xml file:

It is recommended that you store your Google API key in a string resource and reference it from this meta-data tag. From here, you should be able to add a SupportMapFragment to an activity and get started developing your Google Maps application. Refer to this for how to set up a basic Google Maps activity.

Customizing the Google Map

Here are a few brief tips on customizing your app’s Google Map.

Setting size of a Google Map Marker

Markers are covered extensively in the documentation https://developers.google.com/maps/documentation/android-api/marker. Setting a custom marker image can be easily done, but there doesn’t appear to be a way to set marker size directly in the MarkerOptions. Marker size can instead be set by loading and sizing a bitmap of the marker image first.

The bitmap returned by this method can be handed to the MarkerOptions as follows:

Moving the toolbar

The Google Maps toolbar contains buttons for opening a selected location in a navigation app or in Google Maps. If your user interface covers the toolbar in its default location, you can reposition the toolbar by setting the GoogleMap object’s padding.

Setting the bottom padding will move the toolbar.

Getting GPS bounds

To get the GPS bounds of the map that you see on screen, you can use the GoogleMap’s projection as follows:

The LatLngBounds object can be used to get coordinates of the center of the viewing area as well as the bounds. This can be used in conjunction with GoogleMap.OnCameraMoveStartedListener and GoogleMap.OnCameraIdleListener (implemented by the Activity) to update markers if the center of the viewing area has moved a certain amount (or even a certain percentage of the view area’s width).