Google Map Implementation In Android App

Before creating a new project in Android we will see the steps to get Google Maps' API Key, which is used in the app to implement the Google Map.

Login into your Gmail account. Google the word “Google map console in Android.

Open the url.

And click on GET A KEY button.

 
 A pop up will open
   
 Click on CONTINUE and create a new project.
 

API Manager page will display with two options: Overview and Credentials. In credentials, enter the Android API Key name, package name and SHA-1 certificate fingerprint.

Now we will get SHA-1 certificate fingerprint because it is important. For this, first go to the Java folder of C: drive

C:\Program Files\Java\jre1.8.0_25\bin

Then run the command,

   

SHA-1 certificate fingerprint will be created.

   

Enter SHA-1 certificate fingerprint in the "Create Android API Key" option

  
 

And API key will be created finally.

 
 

Created credentials will look like the following:

 

Now create a new project in Android Studio. Click File, New, then New Project

Enter GoogleMapExample as application name and create with a blank activity.

Google maps need some permissions and features.

Added some tags related to user permission in Android-Manifest.xml file. 

  1. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
  2. <uses-permission android:name="android.permission.INTERNET" />  
  3. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  4. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    
  • ACCESS_FINE_LOCATION – It determines user’s location using GPS.
  • INTERNET – It checks internet connection status of mobile phone.
  • ACCESS_NETWORK_STATE – It checks network state whether data can be downloaded or not.
  • WRITE_EXTERNAL_STORAGE – It writes to external storage as Google maps store map data in external storage.

Add a meta-data tag just before activity tag.

  1. <meta-data  
  2.    android:name="com.google.android.geo.API_KEY"  
  3.    android:value="@string/google_maps_key" />  
Complete AndroidManifest.xml file is:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.  <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.  package="com.example.administrator.googlemapexample" >  
  4.      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
  5.      <uses-permission android:name="android.permission.INTERNET" />  
  6.      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  7.      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  
  8.      <application  
  9.        android:allowBackup="true"  
  10.        android:icon="@mipmap/ic_launcher"  
  11.        android:label="@string/app_name"  
  12.        android:theme="@style/AppTheme" >  
  13.          <uses-library android:name="com.google.android.maps"></uses-library>  
  14.    
  15.          <meta-data  
  16.              android:name="com.google.android.geo.API_KEY"  
  17.              android:value="@string/google_maps_key" />  
  18.    
  19.          <activity  
  20.              android:name=".MainActivity"  
  21.              android:label="@string/app_name" >  
  22.              <intent-filter>  
  23.                  <action android:name="android.intent.action.MAIN" />  
  24.    
  25.                  <category android:name="android.intent.category.LAUNCHER" />  
  26.              </intent-filter>  
  27.          </activity>  
  28.      </application>  
  29.    
  30.  </manifest>  
Add google_map_key defined in above tag in strings.xml file under src, main, then res-values folder as:
  1. <string name="google_maps_key">xxxxx-xxxxx-xxxx-xxxx-xxxxx-xxxx-xxxxx-xxx</string>  
In activity_main.xml file add a fragment as:
  1. <fragment  
  2.     android:id="@+id/map"  
  3.     android:name="com.google.android.gms.maps.SupportMapFragment"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:context=".MapsActivity" />  
In MainActivity.java file, extends FragmentActivity and implement a method called OnMapReadyCallback. The complete code of MainActivity.java file is:
  1. package com.example.administrator.googlemapexample;  
  2. import android.support.v4.app.FragmentActivity;  
  3. import android.os.Bundle;  
  4. import com.google.android.gms.maps.CameraUpdateFactory;  
  5. import com.google.android.gms.maps.GoogleMap;  
  6. import com.google.android.gms.maps.OnMapReadyCallback;  
  7. import com.google.android.gms.maps.SupportMapFragment;  
  8. import com.google.android.gms.maps.model.LatLng;  
  9. import com.google.android.gms.maps.model.MarkerOptions;  
  10. public class MainActivity extends FragmentActivity implements OnMapReadyCallback  
  11. {  
  12.     private GoogleMap mMap;  
  13.   
  14.     @Override  
  15.   
  16.     protected void onCreate(Bundle savedInstanceState){  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         /*Obtain the SupportMapFragment and get notified when the map is ready to be used. */  
  20.         SupportMapFragment mapFragment = (SupportMapFragment)  
  21.         getSupportFragmentManager().findFragmentById(R.id.map);  
  22.         mapFragment.getMapAsync(this);  
  23.     }  
  24.   
  25.     @Override  
  26.   
  27.     public void onMapReady(GoogleMap googleMap){  
  28.         mMap = googleMap;  
  29.         /* Show type of Google Map.*/  
  30.         mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);  
  31.         /* Show your current location.*/  
  32.         mMap.setMyLocationEnabled(true);  
  33.         /* Enable zooming controls.*/  
  34.         mMap.getUiSettings().setZoomControlsEnabled(false);  
  35.         /* Enable my location button.*/  
  36.         mMap.getUiSettings().setMyLocationButtonEnabled(true);  
  37.         /* Enable Compass icon.*/  
  38.         mMap.getUiSettings().setCompassEnabled(true);  
  39.         /* Enable Rotate gesture.*/  
  40.         mMap.getUiSettings().setRotateGesturesEnabled(true);  
  41.         /* Enable zooming functionality.*/  
  42.         mMap.getUiSettings().setZoomGesturesEnabled(true);  
  43.         /* Add a marker.*/  
  44.         LatLng delhi = new LatLng(28.737377.091);  
  45.         mMap.addMarker(new MarkerOptions().position(delhi).title("Delhi"));  
  46.         mMap.moveCamera(CameraUpdateFactory.newLatLng(delhi));  
  47.         mMap.animateCamera(CameraUpdateFactory.newLatLng(delhi));  
  48.     }  
  49. }  
Run the app in emulator or device:



Finally location will be displayed as per latitude and longitude defined above.

Read more articles on Android:

Next Recommended Readings