简体   繁体   中英

Google Maps Navigation in Android

I am working with Android and building an app that wants to navigate between 2 points. I already have the points with me. I have the route between them as well on google maps. I just want to navigate between them. Could you please help me on this?

Consider you have two locations source, destination, The below code will redirect you to google navigation, Make it as a method and call when ever you want!.. Let me know if you need any other help

Intent intent = new Intent(Intent.ACTION_VIEW, 

Uri.parse("http://ditu.google.cn/maps?f=d&source=s_d" +
                "&saddr="+source.getLatitude()+
                ","+source.getLongitude()+"&daddr="+
                destination.latitude+
                ","+destination.longitude+
                "&hl=zh&t=m&dirflg=d"));  

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
startActivityForResult(intent, 1);

You can try to use Directions API .

The Google Maps Directions API is a service that calculates directions between locations using an HTTP request.

This service is generally designed for calculating directions for static (known in advance) addresses for placement of application content on a map, however this service is not designed to respond in real time to user input.

A Google Maps Directions API request takes the following form:

https://maps.googleapis.com/maps/api/directions/output?parameters

where output may be either of the following values:

  • json (recommended) indicates output in JavaScript Object Notation (JSON)

  • xml indicates output as XML

To access the Google Maps Directions API over HTTP, use:

http://maps.googleapis.com/maps/api/directions/output?parameters

HTTPS is recommended for applications that include sensitive user data, such as a user's location, in requests.

You need this parameter in getting the directions:

  • origin - The address, textual latitude/longitude value, or place ID from which you wish to calculate directions.

  • destination - The address, textual latitude/longitude value, or place ID to which you wish to calculate directions. The options for the destination parameter are the same as for the origin parameter, described above.

Check also this tutorial for more information.

It's working fine.

 String uri = "http://maps.google.com/maps?f=d&hl=en&saddr="+startingLocation.latitude+","+startingLocation.longitude+"&daddr="+destinationLocation.latitude+","+destinationLocation.longitude;

 Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));

 startActivity(Intent.createChooser(intent, "Select an application"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM