Introduction Android Google Maps
Create a Google Maps project in Android studio following these steps;
- Start Android Studio.
- Create a new project as follows:
- If you see the Welcome to Android Studio dialog, choose Start a new Android Studio project, available under ‘Quick Start’ on the right of the dialog.
- Otherwise, click File in the Android Studio menu bar, then New, New Project.
- Enter your app name, company domain, and project location, as prompted. Then click Next.
- Select the form factors you need for your app. If you’re not sure what you need, just select Phone and Tablet. Then click Next.
- Select Google Maps Activity in the ‘Add an activity to Mobile’ dialog. Then click Next.
‘Add an activity to Mobile’ dialog
- Enter the activity name, layout name and title as prompted. The default values are fine. Then click Finish.
Android Studio starts Gradle and builds your project. This may take a few seconds.
Get a Google Maps API key
Your application needs an API key to access the Google Maps servers. The type of key you need is an API key with Android restrictions. The key is free. You can use it with any of your applications that call the Google Maps Android API, and it supports an unlimited number of users.
Here is a complete tutorial on acquiring an API key. There after copy the API key, and paste the API key into the <string name="google_maps_key">
element in the google_maps_api.xml
file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<resources> <!-- TODO: Before you run your application, you need a Google Maps API key. To get one, follow this link, follow the directions and press "Create" at the end: https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=C2:A2:35:43:2F:A6:6B:26:B1:D6:DD:7C:98:7D:F4:6B:F9:C1:9E:4B%3Bcom.demos.maps You can also add your credentials to an existing key, using this line: C2:A2:35:43:2F:A6:6B:26:B1:D6:DD:7C:98:7D:F4:6B:F9:C1:9E:4B;com.demos.maps Alternatively, follow the directions here: https://developers.google.com/maps/documentation/android/start#get-key Once you have your key (it starts with "AIza"), replace the "google_maps_key" string in this file. --> <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR_KEY_HERE</string> </resources> |
Add the API Key meta-data to the manifest.
1 2 3 4 5 6 7 8 9 10 11 |
<!-- The API key for Google Maps-based APIs is defined as a string resource. (See the file "res/values/google_maps_api.xml"). Note that the API key is linked to the encryption key used to sign the APK. You need a different API key for each encryption key, including the release key that is used to sign the APK for publishing. You can define the keys for the debug and release targets in src/debug/ and src/release/. --> <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key" /> |
Permissions
No permissions are needed to use Google Play Services Maps library, but of course the device must have Internet to load the library.
Add Google Play Services to Your Project
To develop an app that uses Google Maps, it has to make use of the Google Play services APIs. Make the Google Play services APIs available to your app by adding the following dependencies to your build.gradle
file inside your application module directory.
1 2 3 |
dependencies { compile 'com.google.android.gms:play-services:9.2.0' } |
Note: In some cases compiling the entire package of APIs into your app will make it more difficult to keep the number of methods in your app (including framework APIs, library methods, and your own code) under the 65,536 limit. If you encounter this build errors, try using --multi-dex
option.
From version of Google Play services 6.5, you can instead selectively compile Google Play service APIs into your app. For example, to include only the Google Maps API, replace the following line in your build.gradle
file:
1 2 3 |
dependencies { compile 'com.google.android.gms:play-services-maps:9.2.0' } |
Setup the layout
The simplest way to place a map in an application is using the SupportMapFragment
. It’s a wrapper around a view of a map to automatically handle the necessary life cycle needs.
1 2 3 4 5 6 7 8 |
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.demos.maps.MapsActivity" /> |
The maps activity Java file
The Java file that defines the maps activity is named MapsActivity.java
. It extends FragmentActivity
because our layout is a fragment and implements onMapReadyCallback;
a callback interface for when the map is ready to be used.
In order to use GoogleMap , you have to create an object of GoogleMap
. The GoogleMap
is acquired using getMapAsync(OnMapReadyCallback)
. This class automatically initializes the maps system and the view.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.demos.maps; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } /** * Manipulates the map once available. * This callback is triggered when the map is ready to be used. * This is where we can add markers or lines, add listeners or move the camera. In this case, * we just add a marker near Sydney, Australia. * If Google Play services is not installed on the device, the user will be prompted to install * it inside the SupportMapFragment. This method will only be triggered once the user has * installed Google Play services and returned to the app. */ @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in Sydney and move the camera LatLng sydney = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); } } |

Android Google Maps