Android Google Maps API Integration

Total Views : 395
Zoom In Zoom Out Read Later Print

In this tutorial i will show how to integrate android google maps API in your application and customize it according to your own needs. Android Google Maps is an important utility that features in numerous android applications.

Step#1 Android Google Maps API key

To use android google maps API in our application we need to register our app in the google developer console and enable the Google Map API keys.
You get this key via the Google APIs Console. You have to provide your application signature key and the application package name. For you can create project without these data but for key restriction you need them.
Click the Create Project button to create a new project and give it the desired name.

Getting SHA1

The SHA 1 fingerprint is fetched by running the following commands:

Windows

keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

Mac

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

You can also get SHA1 by running signingRepot from android studio.

You can check this video for how you can get API key if you have problem in this.


Step#2 Creating Android Project

1. Create an android project.
2. Add following line in build.gradle file.

build.gradle

implementation 'com.google.android.gms:play-services:12.0.1'


3. Add following tag in you AndroidManifest.xml file.

AndroidManifest.xml

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="API_KEY"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

4. Add following permission for project.

Permission

<uses-permission android:name="android.permission.INTERNET"/>

Project files.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="match_parent"
/>

</RelativeLayout>

MainActivity.java

 package com.prodevsblog.googlemap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
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 MainActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in India, and move the camera.
LatLng gurugram = new LatLng(28.457523, 77.026344);
mMap.addMarker(new MarkerOptions().position(gurugram).title("Marker in India"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(gurugram));
}
}


NOTE: If you run this app now in AVD you may see following error->

GoogleMapExample won't run unless you update Google Play Services.

To solve this you can use older version to match you AVD  play service version or simply update play service in AVD or install newer version.

See More

Latest Photos