【问题标题】:calculate distance & time between two locations in android map V2计算android map V2中两个位置之间的距离和时间
【发布时间】:2014-02-25 05:24:53
【问题描述】:

我正在做一个 android 应用程序,我需要通过在 android mapV2 中给出两个纬度/纬度点来查找两个位置之间的距离和旅行时间。

我使用的示例为http://wptrafficanalyzer.in/blog/driving-distance-and-travel-time-duration-between-two-locations-in-google-map-android-api-v2/

但此示例将敲击位置作为源和目标位置。但我需要提供纬度/经度值并需要计算这两个位置的距离和时间值。我该怎么做?谢谢。

【问题讨论】:

  • 您需要传递 FirstPoint 和 SecondPoint 而不是点击。就是这样。
  • 4 个编辑文本字段,为什么要问这个问题?
  • MD。感谢您的回复。我如何在该示例中传递 lat/lang 值而不是 LatLng 点?非常感谢代码。谢谢

标签: android android-maps-v2


【解决方案1】:

您将通过解析此 google map api 来两个地方: http://maps.googleapis.com/maps/api/geocode/json?latlng="+ lat + "," + lon + &sensor=true"

然后传递地址以获得两点之间的距离。

那么对于距离你应该试试这个,在我的情况下它工作得很好:

 JSONParser jParser = new JSONParser();
                String finalpickmefrom_encoded;
                try {
                    finalpickmefrom_encoded = URLEncoder.encode(finalpickmefrom,"UTF-8");
                    String finaldropmeat_encoded = URLEncoder.encode(finaldropmeat,"UTF-8");
                    addressURLLL = "http://maps.googleapis.com/maps/api/directions/json?origin="+finalpickmefrom_encoded+"&destination="+finaldropmeat_encoded+"&sensor=false";

                    Log.e("Address","URL"+addressURLLL);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 JSONObject json = jParser.getJSONFromUrl(addressURLLL);
                JSONArray addressComp = json.getJSONArray(TAG_SHOW);

                for(int i =0;i<addressComp.length();i++)
                {
                    JSONObject cc = addressComp.getJSONObject(i);

                    JSONArray legss = cc.getJSONArray("legs");

                    for (int j = 0; j < legss.length(); j++) {

                        JSONObject c = legss.getJSONObject(j);
                        JSONObject long_name = c.getJSONObject("distance");
                        String withoutaddvia = long_name.getString("text");
                        String [] strwithout = withoutaddvia.split("\\s+");

                        finalDistanceWithoutAddVia = strwithout[0];
                        String str2 = strwithout[1];

                        String valuess = long_name.getString("value");


                    Log.e("PostalCode","1>>"+long_name);
                    Log.e("PostalCode","2>>"+finalDistanceWithoutAddVia);
                   Log.e("PostalCode","3>>"+valuess);
            }

                }

这里的“finalDistanceWithoutAddVia”是距离。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 2016-01-01
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 2015-10-26
    相关资源
    最近更新 更多