Home » Android » java – Find route with Androids Google Maps API

java – Find route with Androids Google Maps API

Posted by: admin June 15, 2020 Leave a comment

Questions:

I want to be able to display the route between two user-defined geographical points using the Google Maps API for Android. I also want to be able to let the user choose what type of route to display, whether it be walking, cycling, car etc. Additionally, I want to be able to calculate the time and distance it would take to use this route. I’ve tried searching the web and looking at other stackoverflow questions, but to no avail. How would I go about this? How would I be able to code this.

//—-EDIT—-//

I would also like to get traffic information such as busy routes, congestion, etc.

How to&Answers:

Android Google Maps Routing example code using Wrapper library

Use Android Studio Gradle entry:

compile 'com.github.jd-alexander:library:1.1.0'

MainActivity.java

Constants.java

activity_map.xml

Answer:

Here’s some code to help you.

To fetch the data with androidhttpclient, do something like this:

where buildStringIOUtils is:

You can then extract the actual polyline from the JSON-response with something like this:

where the method decodePolyline is this:

You can then add the polyline to the map with this:

To change mode, add this to the url (See https://developers.google.com/maps/documentation/directions/):
&mode=YOUR_MODE

driving (default) indicates standard driving directions using the road network.

walking requests walking directions via pedestrian paths & sidewalks (where available).

bicycling requests bicycling directions via bicycle paths & preferred streets (where available).

transit requests directions via public transit routes (where available).

Edit:
About the “I would also like to get traffic information such as busy routes, congestion, etc.” I have not looked into this, but my code should get you started pretty good.

Edit2:
Found this in the google directions api:
“For Driving Directions: Maps for Business customers can specify the departure_time to receive trip duration considering current traffic conditions. The departure_time must be set to within a few minutes of the current time.”

Answer:

Try the Google Directions API. It’s a web service, which gives turn-by-turn guides in JSON-Format with all information to get from point A to B by car, transit or your feet.

To code that follow the link in the comment of Stochastically.