如何获取两个位置之间的总距离和持续时间以及Google Map中的航点 [英] how to get total distance and duration between two location along with waypoint in google map

查看:310
本文介绍了如何获取两个位置之间的总距离和持续时间以及Google Map中的航点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得两个地点之间的总时长和距离以及航路点.我使用的方法是给我距离和持续时间,但没有航路点.有人可以帮我吗?我的方法如下图所示.

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


Thread thread=new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("https://maps.googleapis.com/maps/api/directions/json?origin=" + lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric&mode=driving&key="+getResources().getString(R.string.google_maps_server_key));
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");
JSONObject duration= steps.getJSONObject("duration");
parsedDuration=duration.getString("text");
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
});

thread.start();

try {
thread.join();
Toast.makeText(this, parsedDistance + " Distance", Toast.LENGTH_SHORT).show();
Toast.makeText(this, parsedDuration + " Duration", Toast.LENGTH_SHORT).show();
} catch (InterruptedException e) {
e.printStackTrace();
}

return parsedDistance;
}

解决方案

public void parseJson(JSONObject jObject) {

        List<List<HashMap<String, String>>> routes = new ArrayList<>();
        JSONArray jRoutes;
        JSONArray jLegs;
        JSONArray jSteps;
        JSONObject jDistance = null;
        JSONObject jDuration = null;
        long totalDistance = 0;
        int totalSeconds = 0;

        try {

            jRoutes = jObject.getJSONArray("routes");

            /* Traversing all routes */
            for (int i = 0; i < jRoutes.length(); i++) {
                jLegs = ((JSONObject) jRoutes.get(i)).getJSONArray("legs");

                                /* Traversing all legs */
                for (int j = 0; j < jLegs.length(); j++) {

                    jDistance = ((JSONObject) jLegs.get(j)).getJSONObject("distance");

                    totalDistance = totalDistance + Long.parseLong(jDistance.getString("value"));

                    /** Getting duration from the json data */
                    jDuration = ((JSONObject) jLegs.get(j)).getJSONObject("duration");
                    totalSeconds = totalSeconds + Integer.parseInt(jDuration.getString("value"));

                }
            }

            double dist = totalDistance / 1000.0;
            Log.d("distance", "Calculated distance:" + dist);

            int days = totalSeconds / 86400;
            int hours = (totalSeconds - days * 86400) / 3600;
            int minutes = (totalSeconds - days * 86400 - hours * 3600) / 60;
            int seconds = totalSeconds - days * 86400 - hours * 3600 - minutes * 60;
            Log.d("duration", days + " days " + hours + " hours " + minutes + " mins" + seconds + " seconds");

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

JSONObject jObject =新的JSONObject(result);

parseJson(jObject);

i want to get total duration and distance between two location along with waypoints. the methodwhich i'm using is giving me distance and duration but without waypoints. so can any one help me? my method look like below...

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


Thread thread=new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("https://maps.googleapis.com/maps/api/directions/json?origin=" + lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric&mode=driving&key="+getResources().getString(R.string.google_maps_server_key));
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");
JSONObject duration= steps.getJSONObject("duration");
parsedDuration=duration.getString("text");
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
});

thread.start();

try {
thread.join();
Toast.makeText(this, parsedDistance + " Distance", Toast.LENGTH_SHORT).show();
Toast.makeText(this, parsedDuration + " Duration", Toast.LENGTH_SHORT).show();
} catch (InterruptedException e) {
e.printStackTrace();
}

return parsedDistance;
}

解决方案

public void parseJson(JSONObject jObject) {

        List<List<HashMap<String, String>>> routes = new ArrayList<>();
        JSONArray jRoutes;
        JSONArray jLegs;
        JSONArray jSteps;
        JSONObject jDistance = null;
        JSONObject jDuration = null;
        long totalDistance = 0;
        int totalSeconds = 0;

        try {

            jRoutes = jObject.getJSONArray("routes");

            /* Traversing all routes */
            for (int i = 0; i < jRoutes.length(); i++) {
                jLegs = ((JSONObject) jRoutes.get(i)).getJSONArray("legs");

                                /* Traversing all legs */
                for (int j = 0; j < jLegs.length(); j++) {

                    jDistance = ((JSONObject) jLegs.get(j)).getJSONObject("distance");

                    totalDistance = totalDistance + Long.parseLong(jDistance.getString("value"));

                    /** Getting duration from the json data */
                    jDuration = ((JSONObject) jLegs.get(j)).getJSONObject("duration");
                    totalSeconds = totalSeconds + Integer.parseInt(jDuration.getString("value"));

                }
            }

            double dist = totalDistance / 1000.0;
            Log.d("distance", "Calculated distance:" + dist);

            int days = totalSeconds / 86400;
            int hours = (totalSeconds - days * 86400) / 3600;
            int minutes = (totalSeconds - days * 86400 - hours * 3600) / 60;
            int seconds = totalSeconds - days * 86400 - hours * 3600 - minutes * 60;
            Log.d("duration", days + " days " + hours + " hours " + minutes + " mins" + seconds + " seconds");

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

JSONObject jObject = new JSONObject(result);

parseJson(jObject);

这篇关于如何获取两个位置之间的总距离和持续时间以及Google Map中的航点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆