google-maps-direction-api-1-1024x682

Implement Google Maps Directions API In Android

Google is providing an easy way to integrate Google Maps Directions API into your android applications. In this post, we are going to learn how to embedding direction API and get direction information and draw a route between two end points on a map by using the Google Maps Directions API.

1. Project Setup

1. Create a new project by going to File ⇒ New Android Project. Fill all the details, select Google Maps Activity and give it appropriate name.

2. Add Play Services dependencies into app/build.gradle and Sync the project.

3. Get API key from Google Developer Console

4. Add Google maps directions API key into your project res/values/google_maps_api.xml file.

5. Add INTERNET And LOCATION permission in AndroidManifest.xml file, this both permissions are required to load map.

6. Add following lines of code into res/values/string.xml

7. Create three packages named model, ui, and utils. Once created, move the MapActivity to ui package.

Below is the final project structure and files required.

2. Create Model Class And Interface

8. Create MapDistance.java class under model package. MapDistance class used for mapping distance between start point and end point.

9. Create another class named MapDuration.java under model package. This model class used to show approximate duration.

10. Create another class named MapRoute.java under model package. This class define the route between two points and containing Lat-Long of route. MapRoute.java is used later in SearchDirection class to find route.

11. Create Interface named SearchDirectionListener under utils package. This is an direction listener interface.

3. Implement Direction API

12. This is the main part to implementing Google Maps Direction API. You can access the Directions API through an HTTP interface, with requests constructed as a URL string. Using lat / long coordinates or text string ot using text strings to identify the locations, along with your API key.

See this example request the driving direction from Delhi to Mumbai.

Example URL : https://maps.googleapis.com/maps/api/directions/json? origin=Delhi&destination=Mumbai&key=<YOUR_API_KEY>

  • So now create SearchDirection.java class under ulits package. Purpose of this class is parse json response from above request and decode poly line using decodePolyLine() method.
  • SearchDirection class will be execute and make server request with origin and destination when user press GO button from the MapActivity. (For more detail check Setup MapActivity).

Step by step instructions of above code.

  • Using createUrl() method create the request url.
  • Using RawDataDownload make request to server.As a successful request server will provide json as a response.
  • We can use parseJson() method to paese server response. The json contain various type of data which is related to route, such as distance, duration, latitude, longitude etc.
  • At last, call decodePolyLine() method. This method will decode polygon line in LatLng formate.

4. Setup MapActivity

13. Create layout names activity_map.xml or rename layout if you have already created the layout file.

This is the main layout of our project containing Google Map, Starting Point and Destination EditText and Go button to find route between two points. In next step we will use activity_map.xml file into MapActivity.java.

13. Create MapActivity.java file into ui package. This activity will display map and has option to add origin and destination field to find route between the points.

5. Run Project

Run and test the app once. Make sure device is having internet connection. Add Origin and destination place and press GO button to see the route, distance and time duration.

Google Maps Directions API

Now all are done ! Write us if you have any question, Happy Coding 🙂