Map Fragment / MapView v2 Quick Start

The Maps APIs for Android have been updated in the latest release of the Google Play Services library and have been rolled out to Android devices with the Play Store. To test it out:

  1. Create a new project
  1. File -> New -> Android Application Project
  2. You can leave all the options with their default values
  3. Choose BlankActivity in the Create Activity screen
  1. Ensure you have the latest version (v2) of the Google Play Services installed.
  1. Select Window > Android SDK Manager or run android at the command line.
  2. Scroll to the bottom of the package list and select Extras > Google Play services. The Google Play services SDK is downloaded to your computer and installed in your Android SDK environment at <android-sdk-folder>/extras/google/google_play_services/
  1. Add the Google Play Services project into your Eclipse workspace.
  1. Click File -> Import..., select Android -> Existing Android Code into Workspace
  2. Browse to and select <android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib
  1. To add the dependency to Google Play Services into your project
  1. Project -> Properties -> Android -> Library, Add -> google-play-services_lib
  1. Update MainActivity.java  to extend FragmentActivity instead of Activity
  2. Check that Google Play Services is available on the device at runtime to protect against error cases

GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

  1. Update res/layout/activity_main.xml and replace the entire contents with

<fragment xmlns:android="http://schemas.android.com/apk/res/android"

 android:id="@+id/map"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 class="com.google.android.gms.maps.SupportMapFragment"/>

  1. The map is controlled through the GoogleMap object, use findFragmentById to keep a reference to the GoogleMap from the layout.

GoogleMap map = ((SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map))

               .getMap();

To use Google Maps API you need an API Key:

  1. Go to the Google APIs Console
  2. In Services enable ‘Google Maps Android API v2’ and agree to the terms
  3. In the left navigation bar, click API Access.
  4. Click Create New Android Key....
  5. In the resulting dialog, enter the SHA-1 fingerprint, then a semicolon, then your application's package name. For example:
  1. BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75;com.example.android.mapexample
  1. If you have debug and production keys then add both in.
  1. To obtain the SHA1 of your debug certificate use keytool:

keytool -list -alias androiddebugkey -keystore ~/.android/debug.keystore -storepass android -keypass android -v

  1. For Windows computers the .android directory is in your user directory
  2. Your new API key will be a 40 character string like AIzbSyBaEqeUpYvlsbUe0OqTPn1-ABVMHVbdNxo
  1. Add the following tag into your AndroidManifest.xml just before the closing </application> tag

<meta-data

   android:name="com.google.android.maps.v2.API_KEY"

   android:value="your_api_key"/>

  1. Your application now also needs the following permissions in the AndroidManifest.xml

<permission

         android:name="your_package_name.permission.MAPS_RECEIVE"

         android:protectionLevel="signature"/>

<uses-permission  android:name="your_package_name.permission.MAPS_RECEIVE"/>

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

  1. Optionally you can include one or both of these permissions to enable auto-location

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

  1. Maps v2 uses OpenGl so the following uses-feature is also required

<uses-feature android:glEsVersion="0x00020000" android:required="true"/>

  1. Test your application, checking the logcat output for any errors.
  2. If it is working try out some of the other GoogleMap options:
  1. mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
  2. mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
  3. mMap.setMyLocationEnabled(true);
  4. more details at

https://developers.google.com/maps/documentation/android/views

Attribution Requirements. If you use the Google Maps Android API in your application, you must include the Google Play Services attribution text as part of a "Legal Notices" section in your application. Including legal notices as an independent menu item, or as part of an "About" menu item, is recommended. The attribution text is available by making a call to GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo.