Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 34  Maps


Getting a Maps API Key


Using the Maps API also requires you to configure an API key in your manifest. To do that, you have
to create your own API key. This API key is used to ensure that your app is authorized to use Google’s
mapping services.


The specifics of how this works can change from time to time, so we recommend you visit Google’s
documentation to get over the initial bump: developers.google.com/maps/documentation/android/
start.


As of this writing, those instructions tell you to start a new project. That is a silly thing to do when
you already have an entire project’s worth of code. We instead recommend that you right-click your
com.bignerdranch.android.locatr package name and select New → Activity → Gallery..., then select
Google Maps Activity to create a new templated maps activity instead. Use the default name for the new
activity.


When you finish those instructions, you will have some new code in your app: a few new manifest
entries, a new string in a new file called values/google_maps_api.xml, and a new activity called
MapsActivity. Make sure that you follow the instructions within google_maps_api.xml to get a
functional API key. The google_maps_key string in your google_maps_api.xml file should look
similar to this, but with a different value for the key:




AIzaSyClrnnYZEx0iYmJkIc0K4rdObrXcFkLl-U

The key is tied to the signing key used to build the app. Our debug signing key is different from yours,
so our key will not work for you. (Our apologies if you took the time to type it out.)


Adding MapsActivity was necessary to build out the template XML, but you do not need
MapsActivity itself. You will be adapting LocatrFragment instead. So delete MapsActivity from your
app and remove its entry from the manifest:


Listing 34.1  Removing MapsActivity’s manifest entry
(AndroidManifest.xml)


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bignerdranch.android.locatr">
...
<application ...>
...
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key"/>


<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">




Free download pdf