I want to show the driving route between two locations in my android app. I want to draw the route only on top of road segments.
There are several answers on stack overflow itself, and all of them were using the same method. Get the directions from start point to destination using google directions API, and draw a polyline across the points returned. Following are some of the answers which uses this method.
https://stackoverflow.com/a/17007360/1015678
https://stackoverflow.com/a/40563930/1015678
But, problem with above method is, when the roads are not straight, the dawn rote is not always on top of the roads, because directions API only returns points where you need to turn from one road to another (at junctions). It doesn't give point details in the bends of the same road segment. So, when I use above method in an area where the roads have so many bends, the route drawn almost always is not on top of road segments.
I found this answer, which does what I need to do, using the javascript API. In this solution, the drawn route nicely follows the roads, similar to the google maps android app. Does someone know whether this is achievable in an android app?
Google Maps android app can nicely draw a route from one point to another, keeping the route on the roads. Does anyone know how Google Maps is doing this? Is it using any other API which is not publicly exposed?
Indeed, you can draw precise route in Google Maps Android API using results provided by Directions API web service. If you read the documentation for Directions API you will see that response contains information about route legs and steps. Each step has a field
polyline
that is described in the documentation asSo, the main idea to solve your issue is to get response from Directions API, loop through route legs and steps, for each step get encoded polyline and decode it to the list of coordinates. Once done you will have a list of all coordinates that compound the route, not only begin and end point of each step.
For simplicity I recommend using the Java client library for Google Maps Web services:
https://github.com/googlemaps/google-maps-services-java
Using this library you can avoid implementing your own async tasks and decoding function for polylines. Read the documentation to figure out how to add the client library in your project.
In Gradle it should be something similar to
I have created a simple example to demonstrate how it works. Have a look at my comments in the code
Please note that for web service you have to create a separate API key, the API key with Android app restriction won't work with web service.
The result of my example is shown in screenshot
You can also download a complete sample project from
https://github.com/xomena-so/so47492459
Don't forget replace the API key with yours.
I hope this helps!
Enable Direction API from Google Console. Replace
API_KEY
inGetPathFromLocation.java
classDirectionHelper.java
DirectionPointListener.java
Use in Activity or Fragment
You can use this library, and it's simple, check example of usage:
And don't forget to add
key
using builder from example, if you getting warnings about keyless access (you should have billing account to use it with key)