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

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

 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()“计算两个位置之间的近似距离(以米为单位),以及可选的两个位置之间最短path的初始和最终方位。 但distanceBetween()和真实的GPS设备或Google导航应用程序返回的结果之间的差异相当大。 例如,我的方法将返回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 

4.947700

Solutions Collecting From Web of "distanceBetween()返回不准确的结果?"

Google导航一般会报告沿方向列表中的一组步骤的驾驶或步行距离,而不是直线距离,这是distanceBetween()报告的范围。

为了与Google Maps保持距离,我使用了Google Directions API和JSONparsing器来检索距离值:

 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; }