I am searching on the Internet how to open google map app that I have installed on my Android device with a Button in my Android application. but I cannot find it. I want to use google map app to navigate to certain place. Can you give me a tutorial, some code, or maybe website link for me to do that?
-
see http://stackoverflow.com/questions/6205827/how-to-open-standard-google-map-application-from-my-application – Roman Pickl Jan 12 '14 at 08:49
3 Answers
You can also simply use http://maps.google.com/maps as your URI
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", sourceLatitude, sourceLongitude, destinationLatitude, destinationLongitude);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
or make sure that the Google Maps app only is used, this stops the intent filter (dialog) from appearing use
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
like so:
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", sourceLatitude, sourceLongitude, destinationLatitude, destinationLongitude);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
or add labels to the locations by adding a string inside parentheses after each set of coordinates like so:
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?saddr=%f,%f(%s)&daddr=%f,%f (%s)", sourceLatitude, sourceLongitude, "Home Sweet Home", destinationLatitude, destinationLongitude, "Where the party is at");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
To use the users current location as the starting point (unfortunately I haven't found a way to label the current location) then just drop off the saddr
parameter as follows:
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?daddr=%f,%f (%s)", destinationLatitude, destinationLongitude, "Where the party is at");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
For completeness, if the user doesn't have the maps app installed then it's going to be a good idea to catch the ActivityNotFoundException, as @TonyQ states, then we can start the activity again without the maps app restriction, we can be pretty sure that we will never get to the Toast at the end since an internet browser is a valid application to launch this url scheme too.
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?&daddr=%f,%f (%s)", 12f, 2f, "Where the party is at");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
try
{
startActivity(intent);
}
catch(ActivityNotFoundException ex)
{
try
{
Intent unrestrictedIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(unrestrictedIntent);
}
catch(ActivityNotFoundException innerEx)
{
Toast.makeText(this, "Please install a maps application", Toast.LENGTH_LONG).show();
}
}

- 20,649
- 15
- 100
- 138
-
Unless I misunderstood your note above, to use the user's current location as the starting point, with a label, simply use googleMap to get the current location and pass it as the sourceLatitude and sourceLongitude – Zvi Jul 20 '16 at 21:27
Or, for check if Google Map App is installed in your device, you can use this code small:
if (intentLocation.resolveActivity(getPackageManager()) != null) {
startActivity(intentLocation);
} else {
Toast.makeText(this, R.string.app_not_available,
Toast.LENGTH_LONG).show();
//or if you are into the
View.OnClickListener
useYourActivity.this
Toast.makeText(YourActivity.this, R.string.app_not_available,
Toast.LENGTH_LONG).show();
}

- 27
- 1
- 6
I know this is a different language, but you may deduct how to do it in your language from what I achieved on android shell:
This shows the map, without a marker:
am start -a android.intent.action.VIEW -d geo:47.6,-122.3
This shows the map, with a marker:
am start -a android.intent.action.VIEW -d geo:47.6,-122.3?q=47.6,-122.3
And then I finally got it with:
am start -a android.intent.action.VIEW -d google.navigation:q=47.6,-122.3

- 21
- 2