David's Blog

Integrating Google Maps API in Android Applications

By David Li on 2025-03-28T06:45:55.000Z

Integrating Google Maps API in Android Applications

Introduction

The Google Maps API is a set of powerful tools that developers can use to integrate Google Maps in their Android applications, providing users with a rich and interactive mapping experience. In this article, we’ll explore the key features of the Android Google Maps API, how to set up your development environment, and how to integrate Google Maps into your Android application.

Prerequisites

To follow along with this tutorial, you’ll need:

  • A Google Cloud Platform account
  • Android Studio installed on your machine
  • Basic knowledge of Android app development

Key Features of the Android Google Maps API

The Android Google Maps API offers a range of features for developers to create engaging and interactive map experiences in their applications. Some of the key features include:

  1. Map types: The API supports various map types such as normal, hybrid, satellite, and terrain.
  2. Markers: Add custom markers to your map to represent points of interest.
  3. Polylines and polygons: Draw lines and shapes on the map to highlight routes, regions, or areas.
  4. Info windows: Display additional information about a location when a marker is tapped.
  5. Geocoding and reverse geocoding: Convert between addresses and geographic coordinates.
  6. Location tracking: Access the user’s current location and track their movements on the map.

Setting Up Your Development Environment

Before you can start using the Google Maps API in your Android app, you’ll need to enable the API in the Google Cloud Platform Console and obtain an API key.

Step 1: Enable the Google Maps API

  1. Sign in to your Google Cloud Platform account at https://console.cloud.google.com/.
  2. Click on “Select a project” and create a new project or choose an existing one.
  3. Use the search bar to find “Maps SDK for Android” and click on the result.
  4. Click “Enable” to enable the Maps SDK for your selected project.

Step 2: Obtain an API Key

  1. After enabling the Maps SDK, click on “Credentials” in the left-hand menu.
  2. Click “Create credentials” and choose “API key” from the dropdown menu.
  3. Copy the generated API key, which we’ll use later in our Android application.

Integrating Google Maps in Your Android Application

Now that we have our API key, let’s see how to add a Google Map to an Android application.

Step 1: Add Dependencies

Open your Android Studio project and add the following dependency to your app’s build.gradle file:

dependencies {
    implementation 'com.google.android.gms:play-services-maps:17.1.0'
}

Step 2: Update the AndroidManifest.xml

In your AndroidManifest.xml file, add the following permissions and meta-data:

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

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

Replace YOUR_API_KEY with the API key you obtained earlier.

Step 3: Add a Map to Your Layout

Update your activity’s layout file to include a SupportMapFragment:

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" />

Step 4: Implement the MapActivity Class

In your activity’s Java or Kotlin file, implement the OnMapReadyCallback interface and override the onMapReady method:

import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;

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 at a specific location
        LatLng location = new LatLng(-33.852, 151.211);
        mMap.addMarker(new MarkerOptions().position(location).title("Marker Title"));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 10));
    }
}

In the example above, we first obtain a reference to the SupportMapFragment from the layout and call getMapAsync() to load the map. Once the map is ready, the onMapReady() method is called, where we can add markers, polylines, or change the map type, among other things.

In this example, we add a marker at a specific location (latitude -33.852 and longitude 151.211) with a title, and move the camera to that location with a zoom level of 10.

Conclusion

In this article, we’ve covered the basics of integrating the Android Google Maps API into your application. With these steps, you can now create rich and interactive map experiences for your users. Additionally, you can explore more advanced features like location tracking, geocoding, and custom styling to further enhance your application’s mapping capabilities.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-11-18.