我正在我的安卓应用程序中使用谷歌地图API v2,我能够显示地图并在上面放置标记,但现在我遇到了一个问题,需要找出放置在地图上的两个标记或点之间的距离,我已经查看了文档,但在这种情况下没有找到任何有用的信息。
如果有人知道如何解决这个问题,请帮助我。
谢谢
我正在我的安卓应用程序中使用谷歌地图API v2,我能够显示地图并在上面放置标记,但现在我遇到了一个问题,需要找出放置在地图上的两个标记或点之间的距离,我已经查看了文档,但在这种情况下没有找到任何有用的信息。
如果有人知道如何解决这个问题,请帮助我。
谢谢
这是一个简单完美的代码,用于计算行程时间和距离
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);
}
你可以使用这个函数。我在我的项目中使用过它。
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;
}
Location l2 = new Location("Two")
- Karue Benson Karue获取两个点之间的距离,请试用以下代码...
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();
}
这是一个旧问题,有旧的答案。我想强调一种更新的计算两点之间距离的方法。现在我们应该熟悉实用类"SphericalUtil"。您可以使用它来获取距离。
double distance = SphericalUtil.computeDistanceBetween(origin, dest);
解析 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);
}
}