Google Maps Android API v2 – Tutorial

Google Maps Android API v2, you can add google maps to your android application. It handles access to Google Maps servers, data downloading, map display, and response to map gestures. API’s can be used for further customizaton such as add markers, polygons, and overlays to a basic map, and to change the user’s view of a particular map area.

MapsSample_Markers
MapsSample_Markers
MapsSample_NormalView
MapsSample_NormalView
MapsSample_SatelliteView
MapsSample_SatelliteView
MapsSample_TerrainView
MapsSample_TerrainView
MapSamples_PoluLine
MapSamples_PoluLine
MapsSampe_CameraMove
MapsSampe_CameraMove
MapsSample_CircleView
MapsSample_CircleView
MapsSample_HybridView
MapsSample_HybridView
MapsSample_MainScreen
MapsSample_MainScreen

1. Configure Google Play Services

For maps api support, Google Play Services library is required to add in project. Refer to Google Play Services Setup.

If you are an eclipse user, Go to menu Window -> Android SDK Manager -> Extras. Install Google Play Service library from there. It will be downloaded to sdk\extras\google\google_play_services\libproject\google-play-services_lib. Import this library project to Eclipse and add as library project for your Google Maps Demo Sample Application (Project->Properties->Android->Add Library).

Addition of the Google Play services version to your app’s manifest is required. Edit your application’s AndroidManifest.xml file while creating android application later on, and add the following declaration within the <application> element. This embeds the version of Google Play services that the app was compiled with.

2. Generating SHA1 using java keytool for your signature key

For getting Google Maps api key, we need to generate SHA-1 fingerprint using java keytool. Open your command prompt and execute the below mentioned command to generate SHA-1 fingerprint. If keytool is not recognized, you can find it in bin directory of installed jre. In my case “C:\Program Files\Java\jre7\bin”.keytool -list -v -keystore “%USERPROFILE%\.android\debug.keystore” -alias androiddebugkey -storepass android -keypass android

From output note down SHA1 output, “FD:0E:04:E9:99:28:B9:3D:E7:AC:75:AF:6E:2B:F6:E7:CD:EE:CA:96” in my case.

3. Generating Google Maps API Key

We need to obtain api key from Google APIs Console page.. Login using your Google id, you will get option to create a new project if no projects exist. Create new project if required. Navigate to APIs & Auth->APIs. Browse for Google Maps Android API v2 and turn on this api.

Now navigate to APIs & Auth->Credentials, select Create new key option and opt for Android Key. Input your SHA1 fingerprint and application package name seperated by semi colon(;). It will generate API Key for your android application. Note down this key as it will be required to mention in manifest of android application.

4. Creating Google Maps Android API v2 Sample Application

Create new Android Application Project. Select Navigation Drawer Activity while selecting for activity type in project wizard as we will user navigation drawer to demonstrate different map options.

4.1 Preparing Application Manifest file

We need to add few required permissions and features in our manifest file.
android.permission.INTERNET to download map tiles from Google Maps servers.
android.permission.ACCESS_NETWORK_STATE to determine whether data can be downloaded.
android.permission.WRITE_EXTERNAL_STORAGE to cache map tile data in the device’s external storage area.
android.permission.ACCESS_COARSE_LOCATION to use WiFi or mobile cell data (or both) to determine the device’s location
android.permission.ACCESS_FINE_LOCATION to determine the device’s location to within a very small area.

Make sure to add Google Maps and Google Play services meta data as well.

4.2 Preparing Layout Files

We will not modify auto generated layout files. activity_main.xml will have DrawerLayout as top-level content view and FrameLayout which will act as container for Google MapView.

fragment_navigation_drawer.xml will have ListView to show map options in navigation drawer list.

4.3 Preparing SRC Files

In auto generated NavigationDrawerFragment.java, define string array for titles of navigation drawers items. We will use navigation drawer selected index to update Google MapView with corresponding options.

Use above defined MAP_VIEW in ArrayAdapter objects on list view. Modify onCreateView as below.

MainActivity.java will implement NavigationDrawerCallbackslbacks for listening to navigation drawer item selection and OnMapReadyCallback for callback of map ready event which will provide non-null GoogleMap object.

Whenever navigation drawer item is selected, we will create a new instance of com.google.android.gms.maps.MapFragment and call getMapAsync(OnMapReadyCallback callback) to set a callback object which will be triggered when the GoogleMap instance is ready to be used. You should ensure that getMapAsync is called from main thread and callback should also be executed on main thread. In the case where Google Play services is not installed on the user’s device, the callback will not be triggered until the user installs it.

Based upon current index of navigation item, GoogleMapOptions will be used to define map type and toggling other features like compass, zoom gestures and controls etc.

Once we get callback that GoogleMap is ready, you will get object of GoogleMap with which we can customize map view with options like adding marker, drawing lines/circle and camera animations.

Adding Marker on GoogleMap

MarkerOptions can be added at any particular latitude and longitude. Yon can set its title, alpha value, marker icon color etc. Snippet content will be shown in marker info window when marker is clicked. We need to ensure to moveCamera to your coordinates with required zoom level (9 in below example).

Drawing lines on GoogleMap

PolylineOptions can be used to draw lines between different coordinates, you can define color for your polyline. Below example will show rectangular shape in blue color.

Drawing circle on GoogleMap with specified radius

CircleOptions is used to draw circle around particular coordinate in map view. You can specify circle radius in meters and stroke color of circle boundary.

Animating camera from location to another

GoogleMap provides camera api’s like moveCamera() to move it to specific location and animateCamera() for camera animations like zoom in/out or to a specific CameraPosition.

Final code for MainActivity.java will be as below after adding all the functionality.

In this tutorial, we have used MapFragment to show GoogleMap view programmatically. We can define MapFragment in our layout as well and use it similar to other fragment. We can find fragment using its id with FragmentManager and carry on same tasks as above. We just need to ensure that android:name attribute defines full class path of MapFragment as below. We can define map attributes as well. In order to use these custom attributes within your XML layout file, you must add the map namespace declaration as below.

You may also like...

Leave a Reply

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