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 NewActivityGallery..., 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:

<!-- Our signing key (not yours) -->
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">
        AIzaSyClrnnYZEx0iYmJkIc0K4rdObrXcFkLl-U</string>

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">
        </activity>
    </application>

</manifest>

As of this writing, the template will also automatically add the com.google.android.gms:play-services artifact to your dependencies. Unfortunately, this artifact is gigantic and will immediately cause you to blow out your method limit budget and fail to build. (Basic Android apps currently support up to 65,536 methods in an APK. If you need more than that, you will need to use multidex, a topic that is beyond the scope of this book.)

Fix this by removing the services dependency (but leave play-services-location and play-services-maps).

Listing 34.2  Removing Play Services dependency (app/build.gradle)

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.google.android.gms:play-services-location:10.0.1'
    compile 'com.google.android.gms:play-services-maps:10.0.1'
    compile 'com.google.android.gms:play-services:10.0.1'
    testCompile 'junit:junit:4.12'
}

With that, you are ready to go.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.154.106