2021/08/01

Implicit intent to start a map activity without specifying any location

by KKHTW



When you want your app to start a map activity, you can use an implicit intent in Android development.

Many examples found online show how to do this, by using Intent.ACTION_VIEW. However, most of them show the approach with a specific URI, letting the user open a map app with the specified coordinates of longitude and latitude.

But what if you want to let the user just launch (open) a map activity in another app on the device, without specifying any location?

The solution is quite straightforward: just leave the coordinates blank.

A code example in Kotlin:


private fun openMap() {
val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse("geo:,")
}
try {
startActivity(intent)
} catch (ex: ActivityNotFoundException) {
// Handle exception(s) here
}
}
view raw openMap.kt hosted with ❤ by GitHub


Android Apps


About Me