Android Google Maps Simple Usage — Google Directions API
What Google Directions API could do?
Simply speaking, Google Directions API provides the route between two position. Users could draw their route by choosing walk, drive or transit to be their way to traffic. This API not only provides the route but also the detail of every move.
Here is an example of Uber.
Uber provides the route for users so that they could know the relative position between them and the driver.
This feature helps users and drivers to save their time to communicate.
So, let’s start to fetch a simple request from Google Directions API.
Enable Directions API
Same as the Places API, we have to enable it before request it.
Reference here to enable it.
Directions API Documents
Google provides a lot of parameters for directions API, the documents tell everything. But we only need origin, destination and key in this section. Due to the fact that the demand is drawing the markers and route from Time Square to Chelsea Market.
https://maps.googleapis.com/maps/api/directions/json?origin=Time+Square&destination=Chelsea+Market&key=YOUR_API_KEY
Retrofit setting
RetrofitClient
This class don’t need to change because Places API and Directions API have the same domain.
GoogleMethods
Putting only required parameters(origin, destination, key) likes the example. Directions API will response the data we need.
Note: About the “mode” parameter, which is also an optional parameter. Default is car, so this example will draw the car route.
Model
Same as the Places API, creating Directions model by the plugin.
And create a custom Route model to save to origin info, destination info and poly line(Optional).
Request Directions API
We have to parse the JSON data after we fetch the Directions API.
Here are what we need.
1. Latitude and longitude of origin. (routes -> legs ->start_location)
2. Latitude and longitude of destination. (routes -> legs ->end_location)
3. Overview poly line. (routes -> overview_polyline -> points)
Android Maps Utils
Before drawing a line on the maps, this library should be importing.
This open-source library contains utilities that are useful for a wide range of applications using the Google Maps Android API.
implementation 'com.google.maps.android:android-maps-utils:0.5'
MapsController
In this section, I create a global list named mRouteMarkerList to store the status of the origin and destination markers and a global Polyline named mRoutePolyline so that could clear them.
private var mRouteMarkerList = ArrayList<Marker>()
private lateinit var mRoutePolyline: Polyline
setMarkersAndRoute
Let’s focus on the Line11 to the Line 17. Those are the code how draw a poly line on the maps.
Same as markers, drawing the route with PolylineOptions and Polyline.
clearMarker
This function helps to clear the markers and route we have generated.
MapsFactory
At the last section, we had created a function named autoZoomLevel in MapsController. But now I will separate that function from it and add another two functions to draw marker and route.
drawMarker
This function is able to make the effect like this.
Using Canvas and Paint to draw a text on the marker.
drawRoute
Markers have MarkerOptions. And same as marker. Polyline also has PolylineOptions to custom the shape of the route.
- Android Google Maps Simple Usage — Introduction
- Android Google Maps Simple Usage — Initial Project with Map
- Android Google Maps Simple Usage — Custom Marker and Zoom Function
- Android Google Maps Simple Usage — Google Places API
- Android Google Maps Simple Usage — Google Directions API (you’re here)
- Android Google Maps Simple Usage — Conclusion