NOTE: Refer to Android Google Maps (Google Play Version 15) for latest guide.
Get API Key
Visit Get API Key to create an API Key. You can select an existing Google project or create a new one. Copy the API Key.
Add Google Play services
Make sure you already install Google Play services SDK (Android Studio > Tools > SDK Manager > SDK Tools > Support Repository > Google Repository).
Edit your application module's build.gradle
to add google play dependencies.
apply plugin: 'com.android.application'
android {
...
}
dependencies {
...
// Releases: https://developers.google.com/android/guides/releases
// Entire google play services package
// compile 'com.google.android.gms:play-services:11.0.4'
// Google Maps only, smaller library size and avoid 64K DEX limit.
compile 'com.google.android.gms:play-services-maps:11.0.4'
}
Google Play services 11.2
If you are using Google Play services version 11.2, edit your project's build.gradle
to add maven.google.com
into repositories.
Note: If you don't add maven.google.com
, it will show error Failed to resolve: com.google.android.gms:play-services-maps:11.2.0
.
Note: Goole Play services release note: August 2017 - version 11.2.0 recommend adding maven.google.com
using google()
. This require Android plugin for Gradle version higher than 2.3.3, else it will show error Gradle DSL method not found: 'google()'
.
Note: Goole Play services release note: August 2017 - version 11.2.0 mentioned you need compileSdkVersion of at least 26 (Android O) to use Google Play services 11.2. I manage to use compileSdkVersion 25 to compile and build com.google.android.gms:play-services-maps:11.2.0
.
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
}
}
allprojects {
repositories {
jcenter()
// ADD THIS: require gradle version higher than 2.3.3
// google()
// ADD THIS: works the same as google()
maven {
url 'https://maven.google.com'
}
}
}
Edit your application module’s build.gradle to add google play dependencies.
apply plugin: 'com.android.application'
android {
...
}
dependencies {
...
compile 'com.google.android.gms:play-services-maps:11.2.0'
}
AndroidManifest.xml
Edit AndroidManifest.xml
and add the meta-data
for com.google.android.geo.API_KEY
and com.google.android.gms.version
.
If you need to access user's current location, include the location permissions as well.
Note: android.permission.INTERNET
and android.permission.ACCESS_NETWORK_STATE
is automatically merged into app's manifest at build time, thus don't need to explicitly include them.
Note: Support for OpenGL ES version 2
is automatically merged into app's manifest at build time as well.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="...">
<!-- ADD THIS: If your application accesses the user's current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- ADD THIS: for Google Play services SDK before 8.3 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application ...>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- ADD THIS: Specify your API key -->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
<!-- ADD THIS: Specify the Google Play services version number -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
Edit layout file
Edit you activity's layout file to add SupportMapFragment.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
...>
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
Add code
Add code to retrieve SupportMapFragment and register map callback using getMapAsync
.
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ... SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(GoogleMap googleMap) { LatLng latLng = new LatLng(1.289545, 103.849972); googleMap.addMarker(new MarkerOptions().position(latLng) .title("Singapore")); googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); } }); }
Your first Android Google Maps with a single marker is ready :)