Google Map Intent | Android Studio | Kotlin

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 Java

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 android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.button.MaterialButton

class MainActivity1 : AppCompatActivity() {

    //UI Views
    private lateinit var pinLocationBtn: MaterialButton
    private lateinit var directionOneBtn: MaterialButton
    private lateinit var directionTwoBtn: MaterialButton

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

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

    override fun onCreate(savedInstanceState: Bundle?) {
        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 {
            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 {
            directionFromCurrentMap(latitudeOne, longitudeOne)
        }

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

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

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

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

Output:



Comments

Popular posts from this blog

Picture In Picture | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Kotlin

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