Launch a Map with Implicit Intent

L

The purpose of this tutorial is to learn how to use implicit intent in an Android application. In this tutorial you will create an application, where user can give latitude and longitude values and this place is shown in the Google Maps application. You will use an implicit intent to launch a Google Maps application from the emulator or the device.

An Intent is a messaging object you can use to request an action from another app componentImplicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. 

Project

Create a new project for this tutorial – you can name it for example LaunchAMap. You should read Create a first Android application tutorial, if you are new with Android.

Application UI

Application UI will contain a text information, two edit text fields and one button. User can give latitude and longitude values and a Google Maps application will be launched, when a button is clicked. Edit text fields are only accepting numerical values.

First modify your string.xml string resources file add a new string resources for the text and button objects. Remember, it is always good to separate string’s from the code and UI. This helps you create a localization of your app.

<resources>
    <string name="app_name">Launch Map</string>
    <string name="info_text">Give a location (lat and lng):</string>
    <string name="button_text">Show a Map</string>
</resources>

Modify your project activity_main.xml layout file. Add a new TextView component and use above string resources to show a “Give a location (lat and lng):” text.

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/info_text"
  ...
/>

Add two EditText components, which user will use to give latitude and longitude values. Remember use id attributes to give a unique id’s for the EditText’s, so they can be found from the Kotlin code. Device’s keyboard will show only numbers, if you set inputType to numberDecimal.

<EditText
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:inputType="numberDecimal"
  android:id="@+id/latEditText"
  ...
/>
<EditText
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:inputType="numberDecimal"
  android:id="@+id/lngEditText"
  ...
/>

Add one Button component and use onClick attribute define showMap function, which will be called, when a button is clicked.

<Button
  android:text="@string/button_text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="showMap" 
  ...
/>

Launch a map

When user presses a button, create an URI from latitude and longitude values and show a Google Maps with implicit intent.

Add a following showMap function inside your MainActivity class. You need to use View based variable in parameters even you are not using that. This is how Android’s event system is working from the XML.

// show a map with implicit intent
fun showMap(v: View) {
  // get latitude and longitude values
  val lat = latEditText.text.toString().toDouble()
  val lng = lngEditText.text.toString().toDouble()

  // Build the intent
  val location = Uri.parse("geo:$lat,$lng")
  val mapIntent = Intent(Intent.ACTION_VIEW, location)

  // Verify it resolves
  val activities: List<ResolveInfo> = packageManager.queryIntentActivities(mapIntent, 0)
  val isIntentSafe: Boolean = activities.isNotEmpty()

  // Start an activity if it's safe
  if (isIntentSafe) {
    startActivity(mapIntent)
  } else {
    Toast.makeText(this, "There is no activity to handle map intent!", Toast.LENGTH_LONG).show();
  }
}

EditText components will hold data as a strings, so they need to be converted to doubles first (if you want to check that they are a real numbers). A new intent can be created with ACTION_VIEW and geo: based data with given latitude and longitude values. It is always good the check that there is an application (or applications), which can handle this created intent with Package manager. And finally start that activity with startActivity method.

TEST

Your application should work now. Test it with a different latitude and longitude values. Notice how easily you can launch a different application from the device with implicit intents.

Add comment

Pasi Manninen

Pasi Manninen

Pasi is a mobile and web application developer. Currently working as a senior lecturer in JAMK University of Applied Sciences. Pasi has programming experience over many decades and has taught dozens of courses since the 90's.

Recent Posts

Follow Me