0

Helo! I have a question!

I have permission to use the following code? I want to calculate the distance between two GPS coordinates.

What do you say?

public class ApiDirectionsAsyncTask extends AsyncTask<Double,Double,Double>
{

    double lat1, lng1, lat2,  lng2;


    ApiDirectionsAsyncTask(double lat1, double lng1, double lat2, double lng2)
    {
        this.lat1 = lat1;
        this.lng1 = lng1;
        this.lat2 = lat2;
        this.lng2 = lng2;
    }


    @Override
    protected Double doInBackground(Double... params) {
        StringBuilder stringBuilder = new StringBuilder();
        Double dist = 0.0;
        try {
            String url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + lat1 + "," + lng1 + "&destination=" + lat2 +","+lng2 + "&mode=driving&sensor=false";

            HttpPost httppost = new HttpPost(url);

            HttpClient client = new DefaultHttpClient();
            HttpResponse response;
            stringBuilder = new StringBuilder();


            response = client.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {

            jsonObject = new JSONObject(stringBuilder.toString());

            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");

            Log.i("Distance", distance.toString());
            dist = Double.parseDouble(distance.getString("text").replaceAll("[^\\.0123456789]","") );

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return dist;
    }

    @Override
    protected void onPostExecute(Double aDouble) {
        super.onPostExecute(aDouble);

    }
}
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
András Ferencz
  • 622
  • 1
  • 7
  • 15
  • Potential duplicate: http://stackoverflow.com/questions/14394366/find-distance-between-two-points-on-map-using-google-map-api-v2 – Andy Feb 23 '15 at 17:13

1 Answers1

0

In your main Method

ApiDirectionsAsyncTask  task = new ApiDirectionsAsyncTask();
task.execute();

Change your code like this

public class ApiDirectionsAsyncTask extends AsyncTask<String, Integer, String> {

    private static final String DIRECTIONS_API_BASE = "http://maps.googleapis.com/maps/api/distancematrix/json";

    @Override
    protected String doInBackground(String... params) {
        StringBuilder stringBuilder = new StringBuilder();
        String dist = "0 km";
        try {
            StringBuilder sb = new StringBuilder(DIRECTIONS_API_BASE);
            sb.append("?origins=" + params[0]);
            sb.append("&destinations=" + params[1]);

            HttpPost httppost = new HttpPost(sb.toString());

            HttpClient client = new DefaultHttpClient();
            HttpResponse response;
            stringBuilder = new StringBuilder();


            response = client.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }

        JSONObject jsonObject;
        try {

            jsonObject = new JSONObject(stringBuilder.toString());
            dist = String.valueOf( jsonObject.getJSONArray("rows").getJSONObject(0).getJSONArray("elements").getJSONObject(0).getJSONObject("distance").get("text")) ;

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return String.valueOf(dist);
    }


    @Override
    protected void onPostExecute(String s) {

        super.onPostExecute(s);
        Log.i("Dis", s);
    }

}
Krishan
  • 2,356
  • 6
  • 36
  • 47