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.
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <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.
- <meta-data
- android:name="com.google.android.geo.API_KEY"
- android:value="@string/google_maps_key" />
Complete
AndroidManifest.xml file is:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.administrator.googlemapexample" >
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <uses-library android:name="com.google.android.maps"></uses-library>
-
- <meta-data
- android:name="com.google.android.geo.API_KEY"
- android:value="@string/google_maps_key" />
-
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
-
- </manifest>
Add google_map_key defined in above tag in strings.xml file under src, main, then res-values folder as:
- <string name="google_maps_key">xxxxx-xxxxx-xxxx-xxxx-xxxxx-xxxx-xxxxx-xxx</string>
In activity_main.xml file add a fragment as:
- <fragment
- android:id="@+id/map"
- android:name="com.google.android.gms.maps.SupportMapFragment"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MapsActivity" />
In MainActivity.java file, extends
FragmentActivity and implement a method called
OnMapReadyCallback. The complete code of MainActivity.java file is:
- package com.example.administrator.googlemapexample;
- 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 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;
-
- mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
-
- mMap.setMyLocationEnabled(true);
-
- mMap.getUiSettings().setZoomControlsEnabled(false);
-
- mMap.getUiSettings().setMyLocationButtonEnabled(true);
-
- mMap.getUiSettings().setCompassEnabled(true);
-
- mMap.getUiSettings().setRotateGesturesEnabled(true);
-
- mMap.getUiSettings().setZoomGesturesEnabled(true);
-
- LatLng delhi = new LatLng(28.7373, 77.091);
- mMap.addMarker(new MarkerOptions().position(delhi).title("Delhi"));
- mMap.moveCamera(CameraUpdateFactory.newLatLng(delhi));
- mMap.animateCamera(CameraUpdateFactory.newLatLng(delhi));
- }
- }
Run the app in emulator or device:
Finally location will be displayed as per latitude and longitude defined above.
Read more articles on Android: