使用Google Map API V2查找地图上两点之间的距离

75

我正在我的安卓应用程序中使用谷歌地图API v2,我能够显示地图并在上面放置标记,但现在我遇到了一个问题,需要找出放置在地图上的两个标记或点之间的距离,我已经查看了文档,但在这种情况下没有找到任何有用的信息。

如果有人知道如何解决这个问题,请帮助我。

谢谢


这里可能存在一个误解,你应该重新检查距离和位移之间的差异。据我所知,Google地图API只提供两点之间的位移而不是距离,你可能需要自己处理或计算距离。 - Bitwise DEVS
16个回答

3

这是一个简单完美的代码,用于计算行程时间和距离

  • Step 1;(add this and sync in your gradle)

     implementation 'com.android.volley:volley:1.0.0'  
    
  • Step 2:(write this in your trigger method)

     public void clicked(View view) throws JSONException {  
    
          JSONObject locationJsonObject = new JSONObject();
                 locationJsonObject.put("origin", "54.406505,18.67708");
                 locationJsonObject.put("destination", "54.446251,18.570993");
                 LatlngCalc(locationJsonObject);
     }
    
  • Step 3:(Copy & Paste in your class)

     private void LatlngCalc(JSONObject locationJsonObject) throws JSONException {
    
         RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
         String url = "https://maps.googleapis.com/maps/api/distancematrix/" +
                     "json?origins=" + locationJsonObject.getString("origin") + "&destinations=" + locationJsonObject.getString("destination") + "&mode=driving&" +
                     "language=en-EN&sensor=false" + "&key=" + /*Insert Your API Key Here */;
    
         StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                 new Response.Listener<String>() {
                     @Override
                     public void onResponse(String response) {
    
                         mTextView.setText("Response is: " + response.substring(0, 500));
                     }
                 }, new Response.ErrorListener() {
             @Override
             public void onErrorResponse(VolleyError error) {
                 mTextView.setText("That didn't work!");
             }
         });
         queue.add(stringRequest);  
     }
    

2

你可以使用这个函数。我在我的项目中使用过它。

public String getDistance(LatLng my_latlong, LatLng frnd_latlong) {
    Location l1 = new Location("One");
    l1.setLatitude(my_latlong.latitude);
    l1.setLongitude(my_latlong.longitude);

    Location l2 = new Location("Two");
    l2.setLatitude(frnd_latlong.latitude);
    l2.setLongitude(frnd_latlong.longitude);

    float distance = l1.distanceTo(l2);
    String dist = distance + " M";

    if (distance > 1000.0f) {
        distance = distance / 1000.0f;
        dist = distance + " KM";
    }
    return dist;
}

你把城镇的名称放在了位置类里面? - Karue Benson Karue
此函数用于计算两点之间的距离,而非用于城镇/街道名称。 - Milon
请问,在Location构造函数中使用String有什么作用呢?Location l2 = new Location("Two") - Karue Benson Karue
每个点都有经纬度,假设我们不知道位置名称但可以获取位置数据。这里有一个名为“Two”的位置l2 = new Location("Two"),表示第二个位置将被识别为Two。它是一个标签。 - Milon

2

获取两个点之间的距离,请试用以下代码...

public static float GetDistanceFromCurrentPosition(double lat1,double lng1, double lat2, double lng2)
 {
        double earthRadius = 3958.75;

        double dLat = Math.toRadians(lat2 - lat1);

        double dLng = Math.toRadians(lng2 - lng1);

        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                + Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2)
                * Math.sin(dLng / 2);

        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        double dist = earthRadius * c;

        int meterConversion = 1609;

        return new Float(dist * meterConversion).floatValue();

    }

2

这是一个旧问题,有旧的答案。我想强调一种更新的计算两点之间距离的方法。现在我们应该熟悉实用类"SphericalUtil"。您可以使用它来获取距离。

double distance = SphericalUtil.computeDistanceBetween(origin, dest);

0
使用Google Directions API。

https://maps.googleapis.com/maps/api/directions/json?origin=00.00,00.004&destination=00.00,00.00&key=YOUR_MAP_KEY

解析 API 响应:

JSONArray jsonArrayRoutes = jObject.getJSONArray("routes");
            if (jsonArrayRoutes.length() > 0) {
                JSONObject jsonObjectRoutes = jsonArrayRoutes.getJSONObject(0);

                if (!jObject.isNull("bounds")) {
                    JSONObject jsonObjectBounds = jObject.getJSONObject("bounds");
                    if (!jsonObjectBounds.isNull("northeast")) {
                        JSONObject jsonObjectNorthEast = jsonObjectBounds.getJSONObject("northeast");
                        LatLng latLngNorthEast = new LatLng(Double.parseDouble(jsonObjectNorthEast.getString("lat")), Double.parseDouble(jsonObjectNorthEast.getString("lng")));
                    }
                    if (!jsonObjectBounds.isNull("southwest")) {
                        JSONObject jsonObjectSouthWest = jsonObjectBounds.getJSONObject("southwest");
                        LatLng latLngSouthWest = new LatLng(Double.parseDouble(jsonObjectSouthWest.getString("lat")), Double.parseDouble(jsonObjectSouthWest.getString("lng")));
                    }
                }

                //Distance & Duration
                JSONArray jsonArrayLegs = jsonObjectRoutes.getJSONArray("legs");
                if (jsonArrayLegs.length() > 0) {
                    JSONObject jsonObjectLegs = jsonArrayLegs.getJSONObject(0);

                    JSONObject jsonObjectDistance = jsonObjectLegs.getJSONObject("distance");
                    String distKM = jsonObjectDistance.getString("text");
                    System.out.println("Distance in KM: " + distKM);
                    JSONObject jsonObjectDuration = jsonObjectLegs.getJSONObject("duration");
                    String durTime = jsonObjectDuration.getString("text");
                    System.out.println("Duration: " + durTime);
                }
            }

-6
在Android谷歌地图应用程序中,有一种非常简单的方法可以找到2个位置之间的距离,只需按照以下简单步骤操作:
  1. 当您首次打开应用程序时,请从左上角的下拉菜单中选择“您的时间轴”。
  2. 一旦新窗口打开,请从右上角菜单中选择“添加地点”。
  3. 添加您的地点并将它们命名为点1、点2或任何易于记忆的名称。
  4. 一旦您的地点被添加并标记,请返回到您的谷歌应用程序的主窗口。
  5. 点击右下角的蓝色圆圈和箭头。
  6. 一个新窗口将打开,在顶部您可以看到两个文本字段,您可以在其中添加“起始位置”和“目标位置”。
  7. 单击任何文本字段并键入保存在点3中的位置。
  8. 单击另一个文本字段并添加下一个保存的位置。
  9. 这样做,谷歌地图将计算两个位置之间的距离,并在地图上显示蓝色路径。
祝你好运!

这个问题是关于如何在Android编程中以程序方式查找两点之间的距离。但是,这个答案是错误的。 - Pang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接