distanceBetween() 返回不准确的结果? [英] distanceBetween() returns inaccurate result?

查看:31
本文介绍了distanceBetween() 返回不准确的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

private float getDistanceInMiles(GeoPoint p1, GeoPoint p2) {双 lat1 = ((double)p1.getLatitudeE6())/1e6;双 lng1 = ((double)p1.getLongitudeE6())/1e6;双 lat2 = ((double)p2.getLatitudeE6())/1e6;双 lng2 = ((double)p2.getLongitudeE6())/1e6;浮动 [] dist = 新浮动 [1];Log.i("目的地坐标", "纬度:" + lat2 + ", 经度:" + lng2);Location.distanceBetween(lat1, lng1, lat2, lng2, dist);返回 dist[0] * 0.000621371192f;}

和 JSON 解析器来检索距离值:

private double getDistanceInfo(double lat1, double lng1, String destinationAddress) {StringBuilder stringBuilder = new StringBuilder();双距离 = 0.0;尝试 {destinationAddress = destinationAddress.replaceAll(" ","%20");字符串 url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + lat1 + "," + lng1 + "&destination=" + destinationAddress + "&mode=driving&传感器=假";HttpPost httppost = new HttpPost(url);HttpClient 客户端 = 新的 DefaultHttpClient();HttpResponse 响应;stringBuilder = new StringBuilder();response = client.execute(httppost);HttpEntity entity = response.getEntity();InputStream 流 = entity.getContent();国际b;while ((b = stream.read()) != -1) {stringBuilder.append((char) b);}} catch (ClientProtocolException e) {} catch (IOException e) {}JSONObject jsonObject = new JSONObject();尝试 {jsonObject = new JSONObject(stringBuilder.toString());JSONArray 数组 = jsonObject.getJSONArray("routes");JSONObject 路由 = array.getJSONObject(0);JSONArray 腿 = routes.getJSONArray("legs");JSONObject 步骤 = 腿.getJSONObject(0);JSONObject distance = steps.getJSONObject("距离");Log.i("距离", distance.toString());dist = Double.parseDouble(distance.getString("text").replaceAll("[^\.0123456789]",""));} catch (JSONException e) {//TODO 自动生成的 catch 块e.printStackTrace();}返回距离;}

I use distanceBetween() of Location class to calculate the distance between two points as follows:

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

Documentation says that distanceBetween() "Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them." However the difference between the result returned by distanceBetween() and real GPS device or Google Navigation app is pretty big. For example my method will return 6.2 miles while google maps shows 10 miles for the same location. I double check the coordinates of the starting point p1 and ending point p2 and they seems to be correct. Is that how distanceBetween() method works or Am I doing something wrong? And by the way Is there a way to use Google Place API to retrieve a distance as a JSON response?

Distance calculated by Google Maps: 6.1 miles

And the result of

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

4.947700

解决方案

To get a distance from a Google Maps I have used Google Directions API and JSON parser to retrieve the distance value:

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

这篇关于distanceBetween() 返回不准确的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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