Google Maps Location Redirect

By Kunal Kapoor
17-05-2018
460

The following project demonstrates how to redirect your Android app to a particular location coordinates in the device's Google Maps application.


Overview:


First lets start by making a simple UI contating a Button and a TextView

xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ourcoaching.simplemapsdirections.MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Maps Location"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.805" />

<EditText
android:id="@+id/editText2"
android:layout_width="251dp"
android:layout_height="120dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textMultiLine"
android:text="Name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.469" />


android.support.constraint.ConstraintLayout
>


The edittext will be used to enter the name of place to redirect maps to and button will be used to trigger the redirect action.


Lets code the java file :

First declare the objects of UI elements :

private Button button;
private EditText editText2;

Then instantiate the variables and declare an OnClick listener for the button

button=(Button)findViewById(R.id.button);
editText2=(EditText)findViewById(R.id.editText2);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GPSFunction();
}
});


In the method GPGSFunction(), first read the contents of edittext2 

String location = editText2.getText().toString();

Create a new string in following format 

String GeoLocation="0,0?q="+location;

Create a new Intent

Intent intent = new Intent(Intent.ACTION_VIEW);

Set the URI data to intent

intent.setData(Uri.parse("geo:"+GeoLocation));

And finally call the startActivity to begin the process.

if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}


Complete Java code: MainActivity.java:

package com.ourcoaching.simplemapsdirections;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

private Button button;
private EditText editText2;

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

button=(Button)findViewById(R.id.button);
editText2=(EditText)findViewById(R.id.editText2);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GPSFunction();
}
});
}

private void GPSFunction() {
String location = editText2.getText().toString();

if (location!=null){

String GeoLocation="0,0?q="+location;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("geo:"+GeoLocation));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}
}


Download the code from GitHub



Related Tutorials