How to find path between two points In android Map with time distance?

1.4k views Asked by At

I am new bie to android development,some how I am able to load googlemap in android using MapView ,

Now I want to find distance between two geopoints .

For that I found that it can be done

using json API (Directions Service )

but not able to get url from Api ,which is the best approach to draw path between two places?( including the time distance between places).....

I am following this link as reference http://javapapers.com/android/draw-path-on-google-maps-android-api/ but its not working for me ...

1

There are 1 answers

3
mray190 On

First, you can find straight distance using:

Location loc1 = new Location();
loc1.setLatitude(43.44556);
loc1.setLongitude(88.56567);
Location loc2 = new Location();
loc2.setLatitude(45.44556);
loc2.setLongitude(83.56567);

double distance = loc1.distanceTo(loc2);

Second, in order to find the time it takes, I would suggest using Google Places API. Super easy to use and free

EDIT: Additional details for google places api (which will indeed give you a poly line which you can easily overlay onto your map)

static final String OUT_JSON = "/json";
static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api";
/**
 * Given a destination place, this will calculate the disance and time it will take to get there from your current location
 * Documentation: https://developers.google.com/maps/documentation/directions/
 * @param place destination place
 * @return String array with index 0 the distance text, index 1 the duration text, and index 2 being the ETA
 */
private String[] getRouteDet(LatLng place, double currentLat, double currentLon) {
    final String APPENDAGES = "/directions";
    StringBuilder sb = new StringBuilder(PLACES_API_BASE + APPENDAGES + OUT_JSON);
    sb.append("?key=");
    sb.append(getResources().getString(R.string.google_places_key));
    try {
        sb.append("&origin=");
        sb.append(URLEncoder.encode(Double.toString(currentLat),"utf8"));
        sb.append(",");
        sb.append(URLEncoder.encode(Double.toString(currentLon,"utf8"));
        sb.append("&destination=");
        sb.append(URLEncoder.encode(Double.toString(place.latitude),"utf8"));
        sb.append(",");
        sb.append(URLEncoder.encode(Double.toString(place.longitude), "utf8"));
        sb.append("&departure_time=now");
        sb.append("&mode=");
        sb.append("driving");
        URL url = new URL(sb.toString());
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStreamReader in = new InputStreamReader(conn.getInputStream());
        // Load the results into a StringBuilder
        int read;
        char[] buff = new char[1024];
        StringBuilder jsonResults = new StringBuilder();
        while ((read = in.read(buff)) != -1) {
            jsonResults.append(buff, 0, read);
        }
        // Create a JSON object hierarchy from the results
        JSONObject jsonObj = new JSONObject(jsonResults.toString());
        if (jsonObj.getString("status").equals("ZERO_RESULTS")) return null;
        JSONObject result = jsonObj.getJSONArray("routes").getJSONObject(0).getJSONArray("legs")
                .getJSONObject(0);
        String distance = result.getJSONObject("distance").getString("text");
        String duration = result.getJSONObject("duration").getString("text");
        long durationVal = result.getJSONObject("duration").getLong("value");
        DateFormat formatter = new SimpleDateFormat("h:mm a", Locale.US);
        return new String[]{distance, duration, formatter.format(System.currentTimeMillis()+durationVal*1000)};
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

Polyline code:

PolylineOptions options = new PolylineOptions().width(15).color(Color.RED).geodesic(true);
//Loop through all of your LatLng steps and add each to the polyline:
//    options.add(step);
map.addPolyline(options);