Google Map Intent | Android Studio | Java

Atif Pervaiz
4 min readAug 15, 2023

--

In this article, we will learn some basic usages of Google Map Intent. For Example to pin a specific location (latitude, longitude) to Google Maps, to show directions between a current location to a specific location, to show directions between two specific locations.

Code Snippet

    private void pinLocationMap(String latitude, String longitude){
// Create a Uri from an intent string. Open map using intent to pin a specific location (latitude, longitude)
Uri mapUri = Uri.parse("https://maps.google.com/maps/search/" + latitude + "," + longitude);
Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);
startActivity(intent);
}


private void directionFromCurrentMap(String destinationLatitude, String destinationLongitude){
// Create a Uri from an intent string. Open map using intent to show direction from current location (latitude, longitude) to specific location (latitude, longitude)
Uri mapUri = Uri.parse("https://maps.google.com/maps?daddr="+ destinationLatitude + "," + destinationLongitude);
Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);
startActivity(intent);
}


private void directionBetweenTwoMap(String sourceLatitude, String sourceLongitude, String destinationLatitude, String destinationLongitude){
// Create a Uri from an intent string. Open map using intent to show direction between two specific locations
Uri mapUri = Uri.parse("https://maps.google.com/maps?saddr=" + sourceLatitude + "," + sourceLongitude + "&daddr=" + destinationLatitude + "," + destinationLongitude);
Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);
startActivity(intent);
}

Full Example

We will do the following:

  1. Pin a specific location (latitude, longitude) to Google Maps
  2. Show directions between a current location to a specific location
  3. Show directions between two specific locations.

Step 1: UI in activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp"
tools:context=".MainActivity">

<!--TextView: To show label-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pin specific location in map"/>

<!--MaterialButton: Open map using intent to pin a specific location-->
<com.google.android.material.button.MaterialButton
android:id="@+id/pinLocationBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pin Location" />

<!--TextView: To show label-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Direction from current location to specific location"
android:layout_marginTop="10dp"/>

<!--MaterialButton: Open map using intent to show direction from current location to specific location-->
<com.google.android.material.button.MaterialButton
android:id="@+id/directionOneBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Direction from current"/>

<!--TextView: To show label-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Direction between two specific locations"
android:layout_marginTop="10dp"/>

<!--MaterialButton: Open map using intent to show direction between two specific locations-->
<com.google.android.material.button.MaterialButton
android:id="@+id/directionTwoBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Direction between specific"/>

</LinearLayout>

Step 2: Coding implementation in MainActivity.java

package com.technifysoft.mapintentjava;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.google.android.material.button.MaterialButton;

public class MainActivity extends AppCompatActivity {

//UI Views
private MaterialButton pinLocationBtn;
private MaterialButton directionOneBtn;
private MaterialButton directionTwoBtn;

//Location One: This is just for an example. Get/Use the required location coordinates you want
private String latitudeOne = "31.4842547";
private String longitudeOne = "74.3258446";

//Location Two: This is just for an example. Get/Use the required location coordinates you want
private String latitudeTwo = "31.480912";
private String longitudeTwo = "74.329535";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//init UI Views from linked layout file of this activity i.e. activity_main.xml
pinLocationBtn = findViewById(R.id.pinLocationBtn);
directionOneBtn = findViewById(R.id.directionOneBtn);
directionTwoBtn = findViewById(R.id.directionTwoBtn);

//handle pinLocationBtn click: Open map using intent to pin a specific location (latitude, longitude)
pinLocationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pinLocationMap(latitudeOne, longitudeOne);
}
});

//handle directionOneBtn click: Open map using intent to show direction from current location (latitude, longitude) to specific location (latitude, longitude)
directionOneBtn.setOnClickListener(v-> {
directionFromCurrentMap(latitudeOne, longitudeOne);
});

//handle directionTwoBtn click: Open map using intent to show direction between two specific locations (latitude, longitude)
directionTwoBtn.setOnClickListener(v-> {
directionBetweenTwoMap(latitudeOne, longitudeOne, latitudeTwo, longitudeTwo);
});
}

private void pinLocationMap(String latitude, String longitude){
// Create a Uri from an intent string. Open map using intent to pin a specific location (latitude, longitude)
Uri mapUri = Uri.parse("https://maps.google.com/maps/search/" + latitude + "," + longitude);
Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);
startActivity(intent);
}


private void directionFromCurrentMap(String destinationLatitude, String destinationLongitude){
// Create a Uri from an intent string. Open map using intent to show direction from current location (latitude, longitude) to specific location (latitude, longitude)
Uri mapUri = Uri.parse("https://maps.google.com/maps?daddr="+ destinationLatitude + "," + destinationLongitude);
Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);
startActivity(intent);
}


private void directionBetweenTwoMap(String sourceLatitude, String sourceLongitude, String destinationLatitude, String destinationLongitude){
// Create a Uri from an intent string. Open map using intent to show direction between two specific locations
Uri mapUri = Uri.parse("https://maps.google.com/maps?saddr=" + sourceLatitude + "," + sourceLongitude + "&daddr=" + destinationLatitude + "," + destinationLongitude);
Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);
startActivity(intent);
}
}

Our code is completed, now you can launch the app and press the button to save. You will be able to see the Image in your Gallery in the album name the same as your app name.

Map Intent Options
Pin Specific Location

Google Map Intent | Android Studio | Kotlin

Thank You for reading the article. If you found this article helpful make sure to click 👏 below to applause this article. It means a lot to me.

--

--

Atif Pervaiz

I'm Android & iOS Application developer & graphics Designer. Learning & delivering knowledge by making it easier to understand for others is my hobby & passion.