IMPORTANT: Google Play Services requires that the AVD be running Android API version 17 or higher with the Google APIs system image. As of this writing, API verion 21 is the highest version working in the AVD, and is recommended.
If you're using a real device, it must be Android version 2.3 or higher.
In order to use an AVD for development of apps that use the Google Maps API, we must first verify that our AVD is configured properly. The easiest way to do this is to create an app that simply opens the native map viewer.
Build a simple app that includes a single button. Clicking this button will open the native Google Map viewer. We use an implicit Intent instance to start this native application as follows:
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:36.593920,-121.875661?z=16"));
startActivity(intent);
When creating an implicit Intent, we pass the action and the data which the requested action is applied. The data value is represented as a Uri resource. To specify the geo location, we pass the string value that consists of the geo tag followed by the latitude and the longitude. We can optionally include the zoom level.
If this simple app works properly (i.e. it shows a map), then our AVD is good to go, and we can continue...
This lesson is not about using the native Map app. We already know how to use Intents to launch native apps. This lesson shows how to include a Map view in our own applications. In order to do this, we must first acquire an API key. Google requires that all apps that use the Maps API are registered. To get the API key, we need to register the certificate (keystore) of the AVD we use for development. The default debug keystore should be located in this folder:
Windows: \users\<username>\.android\debug.keystore
Mac: /Users/<username>/.android/debug.keystore
Linux: /home/<username>/.android/debug.keystore
From the command (Terminal) window, enter the following command (enter as one line):
keytool -list -v -alias androiddebugkey -keystore <fullpath_to_debug_keystore> -storepass android -keypass android
If the user name is pepin and the debug.keystore is located in /Users/pepin/.android/ then the command would be
keytool -list -v -alias androiddebugkey -keystore /Users/pepin/.android/debug.keystore -storepass android -keypass android
If all went as is should, you should be greeted with results similar to this:
If the keytool command is not recognized by the terminal, it is probably because the proper PATH variable was not set when the Java JRE was installed on your machine. In that case, just use the full path to the keytool.exe executable (located in your JRE install folder. For example, to run the keytool on my Windows machine using its absolute path, I would use this:
"C:\Program Files (x86)\Java\jre1.8.0_40\bin\keytool.exe" -list -v -alias androiddebugkey -keystore /Users/pepin/.android/debug.keystore -storepass android -keypass android
Once you've successfully ran the keytool, you will see a list of Certificate fingerprints. The one we're interested in is the SHA1 fingerprint. In the sample screenshot above, the value is:
48:48:E6:86:8C:7C:06:36:60:22:D9:14:25:43:AD:14:FE:B8:1F:19
In addition to generating the SHA1 fingerprint, the keytool command is used for signing the development device for debugging operation. When you release real applications, you must sign them individually. What we're doing here is generating one common debug keystore which we can use for all applications we develop and run in the debug (non-release) mode.
Once you have your SHA1 fingerprint, you must register to use the Google Maps API:
Now that you have the API key, we can use it to build an app that uses Google Maps. For this lesson, we will create a basic app that verifies we have everything configured correctly, and demonstrates some features of the API.
This simple app will only have one time in the layout: a fragment to hold the GoogleMap object.
<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"/>
Running an app that uses Google Maps requires several settings in the Manifest file in order to work properly:
<uses-permission android:name="your_package_name.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="your_api_key"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
Believe it or not, at this point we have a working Map app. Run the app to see what we've accomplished so far.
In order to customize out Map display, we must first instantiate a GoogleMap object. Because we declared the fragment to be a Google Map in our layout file, we can do this by calling the getMap() method of the fragment:
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map))
.getMap();
Once we have a GoogleMap, we can set several options. For example, we can say what type of may to show. By default, the RoadMap style is used. If we want to change this to the Satellite view, we set the may type like this:
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
Other possible map types include:
To change the location of the center of the map, you must move the map 'Camera' to the desired Latitude and Longitude. You can also set the default zoom level at the same time:
LatLng usnaLocation = new LatLng(38.983864, -76.485017);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(usnaLocation, 19));
To add a marker to the map, you use the addMarker() method. This will allow you to set the marker's LatLng, and also give it a title if desired:
map.addMarker(new MarkerOptions().position(usnaLocation).title("MobileOS"));
Finally, we can enable the ability to move the map center to the user's current location on the click of a button:
map.setMyLocationEnabled(true);
This will create a small button at the top of the screen which, when pressed, will cause the map to re-center on the device's current location.
To learn about more of the interesting options possible using the Maps API, go here: https://developers.google.com/maps/documentation/android/views
Android Studio has automated most of the steps in the Creating a Map App section above. To take advantage of this, all you have to do is add a new Google Maps Activity to your app. You should still verify that all the proper permissions and settings have been added, and you'll still have to add your API key to the manifest, but it should save you some time.
Sample App: MapBasic.zip