Integrating Google Maps in Android Studio using Java: A Step-by-Step Guide with Code

Google Maps is one of the most popular location-based services that provides real-time information about geographical locations. It is widely used in mobile applications to provide features like route planning, location-based search, and other location-based services. In this tutorial, we will learn how to integrate Google Maps in an Android Studio project using Java programming language.

Abhinav
2 min readMar 9, 2023

Prerequisites

  • Android Studio installed on your computer
  • A valid Google Maps API key

To integrate Google Maps in an Android Studio project using Java, follow these steps:

Step 1: Create a new project in Android Studio

  • Open Android Studio and select “Create New Project”.
  • Choose a project name and other settings, and click “Finish”.

Step 2: Add Google Play Services to the project

  • In the “build.gradle” file for the app module, add the following dependencies:
implementation 'com.google.android.gms:play-services-maps:17.0.1'
implementation 'com.google.android.gms:play-services-location:18.0.0'
  • Sync the project to download the required libraries.

Step 3: Obtain an API key for Google Maps

  • Go to the Google Cloud Console and create a new project.
  • Enable the “Maps SDK for Android” API for the project.
  • Create an API key and restrict it to your app’s package name and SHA-1 certificate fingerprint.

Step 4: Add a Google Maps fragment to the layout

  • In the XML layout file for your activity, add a fragment with the following attributes:
<fragment
android:id="@+id/map_fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Step 5: Initialize the Google Maps object in the activity

  • In the Java code for your activity, add a reference to the Google Maps fragment and initialize the map object in the onCreate() method:
private GoogleMap mMap;
...
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map_fragment);
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add any necessary initialization code here
}
})

Step 6: Add markers or other objects to the map

  • To add a marker to the map, create a MarkerOptions object and call the addMarker() method on the map object:
LatLng location = new LatLng(latitude, longitude);
MarkerOptions markerOptions = new MarkerOptions()
.position(location)
.title("My Location");
mMap.addMarker(markerOptions);

That’s it! You should now be able to run the app and see the Google Maps fragment with any markers or other objects you’ve added. Don’t forget to include your API key in the app’s manifest file as well:

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY_HERE" />

--

--

Abhinav

Hi, I'm Abhinav, a software engineer with a passion for continuous learning and exploring new technologies.