Kos*_*uta 30 android google-maps android-location
我需要在两个位置之间获得距离,但我需要像图中的蓝线一样得到距离.

我接下来尝试:
public double getDistance(LatLng LatLng1, LatLng LatLng2) {
double distance = 0;
Location locationA = new Location("A");
locationA.setLatitude(LatLng1.latitude);
locationA.setLongitude(LatLng1.longitude);
Location locationB = new Location("B");
locationB.setLatitude(LatLng2.latitude);
locationB.setLongitude(LatLng2.longitude);
distance = locationA.distanceTo(locationB);
return distance;
}
Run Code Online (Sandbox Code Playgroud)
但我得到红线距离.
Chr*_*oot 31
使用Google Maps Directions API.您需要通过HTTP请求指示.您可以直接从Android或通过自己的服务器执行此操作.
例如,从蒙特利尔到多伦多的路线:
GET http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false
Run Code Online (Sandbox Code Playgroud)
你最终会得到一些JSON.在routes[].legs[].distance,你会得到一个像这样的对象:
"legs" : [
{
"distance" : {
"text" : "542 km",
"value" : 542389
},
Run Code Online (Sandbox Code Playgroud)
您还可以直接从响应对象获取折线信息.
Tar*_*ngh 10
正如Chris Broadfoot所说,要解析返回的JSON routes[].legs[].distance
"legs" : [
{
"distance" : {
"text" : "542 km",
"value" : 542389
}
Run Code Online (Sandbox Code Playgroud)
使用:
final JSONObject json = new JSONObject(result);
JSONArray routeArray = json.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONArray newTempARr = routes.getJSONArray("legs");
JSONObject newDisTimeOb = newTempARr.getJSONObject(0);
JSONObject distOb = newDisTimeOb.getJSONObject("distance");
JSONObject timeOb = newDisTimeOb.getJSONObject("duration");
Log.i("Diatance :", distOb.getString("text"));
Log.i("Time :", timeOb.getString("text"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62433 次 |
| 最近记录: |