Home > Java > javaTutorial > How to Launch Google Maps Directions from an Android App Using Intents?

How to Launch Google Maps Directions from an Android App Using Intents?

Linda Hamilton
Release: 2024-12-14 12:30:12
Original
141 people have browsed it

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);
Copy after login

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"));
Copy after login

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"));
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template