• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps GPS Location Distance

How we find exact distance b/w two point on gmap in android ?
I am using this

Code:
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() {
        [USER=1021285]@override[/USER]
        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());
              String  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");
              String  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;
}

but error is comming in this line
String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
 
Last edited by a moderator:
What's the error? If the application crashed, please include the stack trace from the Logcat view.
 
Be careful dabbling in those type of programs you're going to raise some unwanted suspicions.
 
Back
Top Bottom