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

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

Susan Sarandon
Release: 2024-12-20 19:58:13
Original
534 people have browsed it

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

Launching Google Maps Directions with Android Intents

Need to display Google Maps directions from one location to another within your Android app without incorporating the entire map framework? Using intents makes it achievable.

How to Launch Google Maps Directions with Intents:

To launch Google Maps directions using intents, follow these steps:

  1. Define an Intent with the ACTION_VIEW action and set the data URI.
  2. Use latitude and longitude coordinates or street addresses for the source (saddr) and destination (daddr) locations.
  3. Call startActivity(intent) to launch Google Maps.

Example using coordinates:

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

To start navigation from the current location, omit the saddr parameter.

Note: Using street addresses instead of coordinates will present the user with a choice between opening in a browser or Google Maps.

To launch Google Maps navigation directly:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("google.navigation:q=an+address+city"));
Copy after login

Google Maps Intents in 2023

In May 2017, Google introduced a new API that supports universal Google Maps URLs. You can leverage this API with intents as well:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("https://www.google.com/maps/dir/?api=1&origin=source_location&destination=destination_location"));
Copy after login

Note: Replace source_location and destination_location with the appropriate values.

The above is the detailed content of How to Launch Google Maps Directions from Your Android App Using Intents?. For more information, please follow other related articles on the PHP Chinese website!

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