Home >Java >javaTutorial >How to Launch Google Maps Directions from an Android App Using Intents?
Integrating Google Maps Directions with Intents on Android
Question: How can an Android application launch Google Maps directions between two points without integrating the Maps app into the application?
Answer:
Yes, this is achievable through the use of an Intent. The following code snippet demonstrates how it's done:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345")); startActivity(intent);
The URI provided in the snippet specifies the start and destination points (saddr and daddr parameters, respectively) in latitude and longitude format. To initiate navigation from the current location, simply remove the saddr parameter and value.
Alternatively, an actual street address can be used instead of coordinates, although this approach will prompt the user with a dialog asking them to select between opening the navigation in Google Maps or a browser.
For direct navigation using Google Maps, the following Intent can be utilized:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("google.navigation:q=an+address+city"));
Update (May 2017):
Google introduced a new API for cross-platform Google Maps URLs, which allows for more flexibility in creating Intents for directions. This API can also be integrated with Intents using the following syntax:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("https://www.google.com/maps/dir/?api=1&origin=20.344,34.34&destination=20.5666,45.345"));
The above is the detailed content of How to Launch Google Maps Directions from an Android App Using Intents?. For more information, please follow other related articles on the PHP Chinese website!