首页 文章

如何获得在地理编码api android中选择的区域的周长?

提问于
浏览
1

我想问一下如何实现获取地理编码api中的坐标,就像这个
geocoding api perimeter
那时我能够得到地理编码api的jsonresult,它具有几何" : { " lat " : { "东北" : { " 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}}},

在 Map 中使用这个外围可能是最好的部分?

1 回答

  • 4

    您可以使用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);
    }
    

相关问题