LatLng Bounds包含()始终返回false

时间:2017-08-03 20:19:13

标签: android google-maps

我正在尝试检查用户是否在结束位置的边界(lat lng对象),即使当前位置=结束位置,contains函数也总是返回false。所有坐标都正确更新。我是否正确使用LatLngBounds?知道如何解决它或使用Bounds的替代方法吗?

protected void changeStep() throws JSONException {

    Log.i(TAG, " =========== changeStep ========= ");

    int i = 0;
    while (i < routes.size()) {

        //coordinates for end location
        Double latEndLoc = routes.get(i).endLocation.lat;
        Double lngEndLoc = routes.get(i).endLocation.lng;
        LatLng endLoc = new LatLng(latEndLoc,lngEndLoc);

        //setting bounds for end location
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        builder.include(endLoc);
        LatLngBounds bounds = builder.build();

        Log.i("endLoc", endLoc.toString());
        Log.i("bounds center", bounds.getCenter().toString());
        Log.i("currentLoc", currentLocation.toString());


        //checking if users current location is within bounds of end location
            if (bounds.contains(currentLocation)) {

                Log.i(TAG, " =========== enteredIfBranch  ========= ");

                setText(i + 1);
                return;

            }


        i++;

    }

}

2 个答案:

答案 0 :(得分:1)

至少需要两个地点。

    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    builder.include(somewhere);
    builder.include(endLoc);
    LatLngBounds bounds = builder.build();

答案 1 :(得分:0)

builder.include(endLoc);

这应该是“包括()”:

builder.including(endLoc);