Google Map Intent | Android Studio | Java

How to open Google Maps with directions to a specific location (Latitude, Longitude)?

You may have a scenario when you want to open a specific location on Google Maps by clicking a button. In this tutorial, we will input the latitude and longitude and by clicking a button we will open that location in Google Maps. 

>>Check For Kotlin

Complete Code

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>

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);
    }
}

Output:



Comments

Popular posts from this blog

Manage External Storage Permission | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Java

Add a Back Button to Action Bar Android Studio (Kotlin)