1

i want to display route on google map, i know for that direction api is used. further more i want time duration and distance between two points, that is also we can get from direction api. but next step is to calculate location distance and time duration as one person from point A is moving towards point B continuously, then i only want time duration and distance. so the question is...

1) for getting continuous duration and distance without route information which api to be used? distance matrix or direction api? what is difference between them? i'm using direction api now. should i use distance matrix api?

2) i'm using diraction api without passing key...https://maps.googleapis.com/maps/api/directions/json?origin=23.0509738,72.5193273&destination=23.046219,72.515696&sensor=false&units=metric&mode=driving it is giving me same result as you can see in example. so do i use it without passing key? or i have to pass the key along with url?

public String getDistance(final double lat1, final double lon1, final double lat2, final double lon2){
    String parsedDistance;
    String response;

    Thread thread=new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          URL url = new URL("http://maps.googleapis.com/maps/api/directions/json?origin=" + lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric&mode=driving");
          final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setRequestMethod("POST");
          InputStream in = new BufferedInputStream(conn.getInputStream());
          response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");

          JSONObject jsonObject = new JSONObject(response);
          JSONArray array = jsonObject.getJSONArray("routes");
          JSONObject routes = array.getJSONObject(0);
          JSONArray legs = routes.getJSONArray("legs");
          JSONObject steps = legs.getJSONObject(0);
          JSONObject distance = steps.getJSONObject("distance");
          parsedDistance=distance.getString("text");
        } catch (ProtocolException e) {
          e.printStackTrace();
        } catch (MalformedURLException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        } catch (JSONException e) {
          e.printStackTrace();
        }
      }
    });

    thread.start();

    try {
      thread.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    return parsedDistance;
}
Tushar Kotecha
  • 764
  • 2
  • 7
  • 27

1 Answers1

0

you can use the Distance Matrix API when your solution requires distance and travel time between a large list of origin-destination points.

The difference between them is "a distance matrix API computes distances and ETAs between any given set of origins and destinations. A directions API finds the best possible route, based on distance or ETA, between any two given point. so you need to use both API as per your requirement.

2. while using any service of API you must be specify the API KEY. you have to pass the key with URL.