【问题标题】:getting wrong distance from Geocoder in Android在 Android 中与 Geocoder 的距离错误
【发布时间】:2012-09-03 08:18:52
【问题描述】:

我有 2 个文本框,我在其中提供源地址和目标地址。

1) 来源 = SHAHIBAUG UNDER BRIDGE, Shahibagh, Ahmedab​​ad, Gujarat

2) 目的地 = CG Road, Shreyas Colony, Navrangpura, Ahmedab​​ad, Gujarat

距离 = 4.6(来自 Google 地图) && 2.6656852(使用位置的 distanceTo 方法)

final ProgressDialog dialog = ProgressDialog.show(AutoActivity.this, "Fare", "test");

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Location source_location = new Location("");
                        Location destination_location = new Location("");
                        Geocoder geoCode = new Geocoder(getApplicationContext());

                        source_location.setLatitude(geoCode.getFromLocationName(source_input.getText().toString(), 1).get(0).getLatitude());
                        source_location.setLatitude(geoCode.getFromLocationName(source_input.getText().toString(), 1).get(0).getLongitude());
                        source_location.set(source_location);

                        destination_location.setLatitude(geoCode.getFromLocationName(destination_input.getText().toString(), 1).get(0).getLatitude());
                        destination_location.setLatitude(geoCode.getFromLocationName(destination_input.getText().toString(), 1).get(0).getLongitude());
                        destination_location.set(destination_location);

                        float distance = source_location.distanceTo(destination_location)/1000;
                        System.out.println("Distance == " + distance);
                        kmVal = BigDecimal.valueOf(distance);
                        calculateFares();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (dialog!=null) {
                            dialog.dismiss();   
                        }
                    }
                }
            }).start();

我不知道为什么我的距离有误。我对我的代码有一个疑问,那就是我使用了 getFromLocationName 方法,并且我传递了 1 个参数以获得一个结果。这有什么区别吗?任何人有任何想法请帮忙。

【问题讨论】:

  • 尝试计算与源 lat,lng 和目标 lat,lng 的距离
  • 我需要这两个位置的道路距离。是否可以通过 android 中可用的 Geocoder 和 Location API 或必须使用 Google Maps。

标签: android google-maps location google-geocoder


【解决方案1】:

谷歌地图为您提供道路距离,地理编码器为您提供乌鸦飞行的距离。

【讨论】:

  • 嗯..对..那么有什么解决办法吗。
  • 解决方案是什么?这些是不同的东西。你想得到什么?
  • 那要复杂得多。您需要服务来为您提供此类信息。
  • 所以基本上我必须使用谷歌地图服务。如果我错了,请纠正我。我浏览了 API,这就是距离。
  • 我猜你可以使用距离矩阵 api developers.google.com/maps/documentation/distancematrix
【解决方案2】:

使用 Google Maps API 解决了这个问题。希望它会帮助其他人。它为您提供准确的距离。

final ProgressDialog dialog = ProgressDialog.show(AutoActivity.this, "", "Calculating...");

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        float distance = 0.0f;
                        StringBuffer url = new StringBuffer("http://maps.googleapis.com/maps/api/directions/json?origin=");
                        url.append(source_input.getText().toString().replace(" ", "").trim().toString());
                        url.append("&destination=");
                        url.append(destination_input.getText().toString().replace(" ", "").trim().toString());
                        url.append("&sensor=false");

                        DefaultHttpClient httpClient = new DefaultHttpClient();
                        HttpGet httpGet = new HttpGet(url.toString());
                        HttpResponse httpResponse = httpClient.execute(httpGet);
                        HttpEntity httpEntity = httpResponse.getEntity();
                        String line = EntityUtils.toString(httpEntity);

                        JSONObject jsonObject = new JSONObject(line);
                        JSONArray jsonarray = jsonObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs");

                        for (int i = 0; i < jsonarray.length(); i++) {
                            JSONObject obj = jsonarray.getJSONObject(i);
                            distance  = Float.valueOf(obj.getJSONObject("distance").getString("text").replace("km", ""));
                            System.out.println("Distance == " + obj.getJSONObject("distance").getString("text").replace("km", ""));
                        }


                        kmVal = BigDecimal.valueOf(distance);
                        calculateFares();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (dialog!=null) {
                            dialog.dismiss();   
                        }
                    }
                }
            }).start();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多