Google Map Intent - Android Studio - Compose
How to open Google Maps with directions to a specific location (Latitude, Longitude)?
You may have a scenario where 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 JavaMainActivity.kt
package com.technifysoft.myapplication import android.content.Context import android.content.Intent import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.material3.Button import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.technifysoft.myapplication.ui.theme.MyApplicationTheme import androidx.core.net.toUri /** * MainActivityCompose is the main activity of the application, responsible for setting up the UI. */ class MainActivityCompose : ComponentActivity() { /** * Called when the activity is first created. This is where you should do all of your normal static set up: * create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's * previously frozen state, if there was one. */ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { MyApplicationTheme { Scaffold( modifier = Modifier.fillMaxSize(), ) { innerPadding -> MainUI(innerPadding) } } } } } @Composable fun MainUI(innerPadding: PaddingValues) { // Get the current context val context = LocalContext.current //Location One: This is just for an example. Get/Use the required location coordinates you want val latitudeOne = "31.4842547" val longitudeOne = "74.3258446" //Location Two: This is just for an example. Get/Use the required location coordinates you want val latitudeTwo = "31.480912" val longitudeTwo = "74.329535" Column( modifier = Modifier .fillMaxSize() .padding(16.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center ) { Text(text = "Pin specific location in map") Spacer(modifier = Modifier.height(8.dp)) Button( onClick = { pinLocationMap(context,latitudeOne, longitudeOne) }, modifier = Modifier.fillMaxWidth() ) { Text(text = "Pin Location") } Spacer(modifier = Modifier.height(16.dp)) Text(text = "Open Direction from current location to specific location") Spacer(modifier = Modifier.height(8.dp)) Button( onClick = { directionFromCurrentMap(context,latitudeOne, longitudeOne) }, modifier = Modifier.fillMaxWidth() ) { Text(text = "Direction from current") } Spacer(modifier = Modifier.height(16.dp)) Text(text = "Open Direction between two specific locations") Spacer(modifier = Modifier.height(8.dp)) Button( onClick = { directionBetweenTwoMap(context,latitudeOne, longitudeOne, latitudeTwo, longitudeTwo) }, modifier = Modifier.fillMaxWidth() ) { Text(text = "Direction between specific") } } } private fun pinLocationMap(context: Context, latitude: String, longitude: String) { // Create a Uri from an intent string. Open map using intent to pin a specific location (latitude, longitude) val mapUri = "https://maps.google.com/maps/search/$latitude,$longitude".toUri() val intent = Intent(Intent.ACTION_VIEW, mapUri) context.startActivity(intent) } private fun directionFromCurrentMap(context: Context, 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 = "https://maps.google.com/maps?daddr=$destinationLatitude,$destinationLongitude".toUri() val intent = Intent(Intent.ACTION_VIEW, mapUri) context.startActivity(intent) } private fun directionBetweenTwoMap(context: Context, 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 = "https://maps.google.com/maps?saddr=$sourceLatitude,$sourceLongitude&daddr=$destinationLatitude,$destinationLongitude".toUri() val intent = Intent(Intent.ACTION_VIEW, mapUri) context.startActivity(intent) } /** * GreetingPreview is a composable function for previewing the MainUI in Android Studio. * It is annotated with @Preview to enable live preview. * */ @Preview(showBackground = true) @Composable fun GreetingPreview() { MyApplicationTheme { MainUI(PaddingValues()) } }
Comments
Post a Comment