Google is providing Direction Web Service for finding directions between places. You can find the documentation of that REST service here. In this tutorial I am going to explain how to use that service using java.

Program Flow

  1. Form Get request to Google server
  2. Get Response 
  3. Convert Response to Java Pojo Classes
  4. Use those Pojo classes for your own logic
Find the project at below link and execute DirectionFinder.java

Get URL 

https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&avoid=highways&mode=bicycling

Parameters

  1. origin — The address or textual latitude/longitude value from which you wish to calculate directions. If you pass an address as a string, the Directions service will geocode the string and convert it to a latitude/longitude coordinate to calculate directions. If you pass coordinates, ensure that no space exists between the latitude and longitude values.
  2. destination — The address or textual latitude/longitude value from which you wish to calculate directions. If you pass an address as a string, the Directions service will geocode the string and convert it to a latitude/longitude coordinate to calculate directions. If you pass coordinates, ensure that no space exists between the latitude and longitude values.
  3. key — Your application's API key. This key identifies your application for purposes of quota management
  4. mode — specifies the mode of transport to use when calculating directions (driving (default), walking, bicycling)
  5. waypoints — Specifies an array of waypoints. Waypoints alter a route by routing it through the specified location(s). A waypoint is specified as either a latitude/longitude coordinate or as an address which will be geocoded. Waypoints are only supported for driving, walking and bicycling directions. (For more information on waypoints, see Using Waypoints in Routes below.)
  6. avoid — Indicates that the calculated route(s) should avoid the indicated features. This parameter supports the following arguments: tolls, highways, ferries
Still so many parameters are there. You can find them at documentation

JavaCode

In this project, I have created all required pojo classes. Just download the project for quick execution. This is the main java code to trigger Google service and data handling
URL url = new URL("https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&avoid=highways&mode=bicycling");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setRequestMethod("GET");
  String line, outputString = "";
  BufferedReader reader = new BufferedReader(
  new InputStreamReader(conn.getInputStream()));
  while ((line = reader.readLine()) != null) {
       outputString += line;
  }
  System.out.println(outputString);
  DirectionsPojo dp = (DirectionsPojo) JsonGenerator.generateTOfromJson(outputString, DirectionsPojo.class);
  for(Routes route:dp.getRoutes()) {
      System.out.println("----- Route Begins ------");
      for(Legs leg:route.getLegs()) {
          System.out.println("Total Distance "+leg.getDistance().getText());
          for(Steps step:leg.getSteps()) {
              System.out.println(step.getDistance().getText());
              System.out.println(step.getDuration().getText());
          }
      }
      System.out.println("----- Route Ends ------");
  }

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. You do not need to be a programmer to download the plugin to the site. You can do it by yourself. Users will be able to find you more easily if the site will contains a plug-in from Google Maps. You can download it for free and use it without registration, use the service https://elfsight.com/google-maps-widget/ to download. Easily and quickly installed application, with a nice interface and also regular updates.

    ReplyDelete
  3. Games of different genres are available online so people can play the games that they relate with and that they want to play. Download Textme - Free Texting & Calls

    ReplyDelete

Blogroll

Popular Posts