如何获取地理编码apiandroid中选中区域的周长?

How to get the perimeter of the area selected in geocoding api android?

我想请问如何实现在地理编码中获取坐标api是这样的目前我可以得到地理编码的jsonresultapi 有几何形状" : { "bounds":{ "northeast":{ "lat":37.842911, "lng":-85.682537 }, "southwest":{ "lat":37.559684, "lng":-86.07509399999999 } }, "location":{ "lat":37.7030051, "lng":-85.8647201 }, "location_type" : "APPROXIMATE", "viewport":{ "northeast":{ "lat":37.842911, "lng":-85.682537 }, "southwest":{ "lat":37.559684, "lng":-86.07509399999999 } } },

最好用什么部分来实现地图中的这个周长?

您可以使用 Google Maps Android API Utility Library 中的 SphericalUtil.computeLength 方法。此方法接收一个 List<LatLng> 作为参数,并计算路径的长度,因此您的列表将需要包含一个闭合路径。

您可以像这样解码 JSON 并计算周长:

try {
    String jsonString = "{ \"bounds\" : { \"northeast\" : { \"lat\" : 37.842911, \"lng\" : -85.682537 }, \"southwest\" : { \"lat\" : 37.559684, \"lng\" : -86.07509399999999 } }, \"location\" : { \"lat\" : 37.7030051, \"lng\" : -85.8647201 }, \"location_type\" : \"APPROXIMATE\", \"viewport\" : { \"northeast\" : { \"lat\" : 37.842911, \"lng\" : -85.682537 }, \"southwest\" : { \"lat\" : 37.559684, \"lng\" : -86.07509399999999 } } }";
    JSONObject object = new JSONObject(jsonString);

    JSONObject boundsJSON = object.getJSONObject("bounds");
    LatLng northeast = getLatLng(boundsJSON.getJSONObject("northeast"));
    LatLng southwest = getLatLng(boundsJSON.getJSONObject("southwest"));
    LatLng northwest = new LatLng(northeast.latitude, southwest.longitude);
    LatLng southeast = new LatLng(southwest.latitude, northeast.longitude);

    List<LatLng> path = new ArrayList<>();
    path.add(northwest);
    path.add(northeast);
    path.add(southeast);
    path.add(southwest);
    path.add(northwest);
    double perimeter = SphericalUtil.computeLength(path);
} catch (JSONException e) {
    // TODO: Handle the exception
    String a = "";
}

这是解码坐标的getLatLng方法(在上面的代码中使用):

private LatLng getLatLng(JSONObject coordinateJSON) throws JSONException {
    double lat = coordinateJSON.getDouble("lat");
    double lon = coordinateJSON.getDouble("lng");

    return new LatLng(lat, lon);
}