0

有 Location.distanceBetween 方法,但这只是直线。我也研究了谷歌距离矩阵 API,但这似乎相当复杂,尽管它会给我我需要的信息。还有其他方法吗?

4

1 回答 1

1

据我所知,没有。Distance Matrix 是 Google 提供的唯一一项可以为您提供所需信息的服务(汽车、步行、自行车两点之间的距离)。

用法很简单。我已经多次使用它(JSON)并且效果很好。这是获取距离的代码:

String sDistance = "";

            try
            {
                URL googleDMatrix = new URL("http://maps.googleapis.com/maps/api/distancematrix/json?origins="
                        + URLEncoder.encode(params[6].toString(), "UTF-8") + "&destinations="
                        + URLEncoder.encode(params[7].toString(), "UTF-8") + "&language=en-GB&sensor=false&units=imperial");
                URLConnection tc = googleDMatrix.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

                String line;
                StringBuffer sb = new StringBuffer();
                // take Google's legible JSON and turn it into one big string.
                while ((line = in.readLine()) != null)
                {
                    sb.append(line);
                }

                // turn that string into a JSON object
                JSONObject main = new JSONObject(sb.toString());
                // now get the JSON array that's inside that object

                JSONArray rows_array = new JSONArray(main.getString("rows"));
                JSONObject elements_object = rows_array.getJSONObject(0);
                JSONArray elements_array = elements_object.getJSONArray("elements");
                JSONObject distance_object = elements_array.getJSONObject(0);
                JSONObject distance = distance_object.getJSONObject("distance");

                double dist = (distance.getDouble("value") / 1E3) * 0.62;

                sDistance = Double.toString(dist);
            }
            catch (Exception e)
            {
                sDistance = "0";
                e.printStackTrace();
            }

这里可能有一些额外的东西,因为这直接来自我的项目。 params[6] params[7]是计算距离的两个点。

于 2013-09-03T06:09:58.640 回答