11

我使用distanceBetween()Location 类来计算两点之间的距离,如下所示:

private float getDistanceInMiles(GeoPoint p1, GeoPoint p2) {

    double lat1 = ((double)p1.getLatitudeE6()) / 1e6;
    double lng1 = ((double)p1.getLongitudeE6()) / 1e6;
    double lat2 = ((double)p2.getLatitudeE6()) / 1e6;
    double lng2 = ((double)p2.getLongitudeE6()) / 1e6;
    float [] dist = new float[1];
    Log.i("destination coordinates", "Latitude:" + lat2 + ", Longitude: " + lng2);
        Location.distanceBetween(lat1, lng1, lat2, lng2, dist);

    return dist[0] * 0.000621371192f;
}

文档说 distanceBetween() “计算两个位置之间的近似距离,以及可选的它们之间最短路径的初始和最终方位角。” 然而 distanceBetween() 返回的结果与真正的 GPS 设备或谷歌导航应用程序之间的差异是相当大的。例如,我的方法将返回 6.2 英里,而谷歌地图显示同一位置的 10 英里。我仔细检查了起点 p1 和终点 p2 的坐标,它们似乎是正确的。这是 distanceBetween() 方法的工作原理还是我做错了什么?顺便说一句,有没有办法使用 Google Place API 来检索距离作为 JSON 响应?

谷歌地图计算距离:6.1英里

在此处输入图像描述

结果

Location.distanceBetween(41.742964, -87.995971, 41.811511, -87.967923, dist) is 

4.947700

4

2 回答 2

14

Google 导航通常会报告沿路线列表中的一组步骤的驾驶或步行距离,而不是报告的直线距离distanceBetween()

于 2013-01-31T03:50:53.983 回答
8

为了从谷歌地图获取距离,我使用了谷歌方向 API 和 JSON 解析器来检索距离值:

private double getDistanceInfo(double lat1, double lng1, String destinationAddress) {
            StringBuilder stringBuilder = new StringBuilder();
            Double dist = 0.0;
            try {

            destinationAddress = destinationAddress.replaceAll(" ","%20");    
            String url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + lat1 + "," + lng1 + "&destination=" + destinationAddress + "&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;
        }
于 2013-01-31T05:12:05.043 回答