distanceBetween()返回不准确的结果?

Mar*_* S. 10 android google-maps android-location

我使用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;
}
Run Code Online (Sandbox Code Playgroud)

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

Google地图计算的距离:6.1英里

在此输入图像描述

结果

Location.distanceBetween(41.742964, -87.995971, 41.811511, -87.967923, dist) is 
Run Code Online (Sandbox Code Playgroud)

4.947700

Com*_*are 14

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


Mar*_* S. 8

为了与Google地图保持距离,我使用了Google Directions 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;
        }
Run Code Online (Sandbox Code Playgroud)

  • Thanx,我搜索了这个解决方案一段时间了! (3认同)