仅在地面覆盖层上限制相机? Google Map Android API v2

时间:2017-01-12 06:26:16

标签: android google-maps gmsmapview gmsgroundoverlay

我正在尝试向我的用户显示带有标记的地面叠加层。我正在尝试将视图限制为只放置在地图上的此图像。我希望用户只将图像看作放置在地图上的地面叠加层,而不能转到周围的地图。如果他们越过边缘,手势将被阻止。

我想要这样的事情:

enter image description here

我不想要这个: show-only-ground-overlays-map-android或者这个:

enter image description here

我尝试将map.setLatLngBoundsForCameraTarget()设置为我的图像边界,但结果与之前的图像类似......

我接下来尝试的是在地面叠加层周围设置一堆LatLng个对象,并与curScreen.contains(customMapDetectionPoints.get(LatLng Object))一起检查它们是否出现在视口上。它确实有效,但我无法阻止相机超越边缘......

到目前为止我的测试代码:

private GroundOverlay groundOverlay;
private GoogleMap globalMap;
private final int DETECTION_POINTS_CUSTOM_MAP = 20;
private List<LatLng>  customMapDetectionPoints = new ArrayList<>();

//Fully Working as suppose to
@Override
public void onMapReady(GoogleMap map) {
           //Other Stuff...
            LatLngBounds mapBounds = groundOverlay.getBounds();
            map.setLatLngBoundsForCameraTarget(mapBounds);
            globalMap = map;

            LatLng northwest = new LatLng( mapBounds.northeast.latitude,  mapBounds.southwest.longitude);
            LatLng northeast = mapBounds.northeast;
            LatLng southeast = new LatLng( mapBounds.southwest.latitude,  mapBounds.northeast.longitude);
            LatLng southwest = mapBounds.southwest;

            //My ground overlay is rectangle so I don't need to follow a path or something like that
            setDetectionPoints(northwest, southwest);
            setDetectionPoints(northeast, southeast);
            setDetectionPoints(northwest, northeast);
            setDetectionPoints(southwest, southeast);

            map.setOnCameraMoveStartedListener(new GoogleMap.OnCameraMoveStartedListener() {
        @Override
        public void onCameraMoveStarted(int i) {
            LatLngBounds curScreen = globalMap.getProjection().getVisibleRegion().latLngBounds;

            CameraPosition cameraPosition = globalMap.getCameraPosition();

            for (int x =0;x<customMapDetectionPoints.size();x++) {
                if (curScreen.contains(customMapDetectionPoints.get(x))) {
                    cancelMapMovement(cameraPosition);
                     Log.d("OUT", "Ground Overlay is outside viewport");
                    return;
                } else {
                    globalMap.getUiSettings().setAllGesturesEnabled(true);
                    Log.d("IN", "Ground Overlay is inside viewport");
                }
            }

        }
    });

 //Add 20 new location between two location
 //Fully Working as suppose to
 public void setDetectionPoints(LatLng fromPos, LatLng toPos) {

    double pointLatitude = fromPos.latitude;
    double pointLongitude = fromPos.longitude;
    double addingValue;
    if (fromPos.latitude == toPos.latitude) {

        addingValue = (toPos.longitude - fromPos.longitude)/DETECTION_POINTS_CUSTOM_MAP;

        for (int i = 0; i < DETECTION_POINTS_CUSTOM_MAP; i++) {
            pointLongitude += addingValue;
            LatLng pointsPos = new LatLng(pointLatitude, pointLongitude);
            customMapDetectionPoints.add(pointsPos);
        }


    } else if (fromPos.longitude == toPos.longitude) {
        addingValue = (toPos.latitude - fromPos.latitude)/DETECTION_POINTS_CUSTOM_MAP;
        for (int i = 0; i < DETECTION_POINTS_CUSTOM_MAP; i++) {

            pointLatitude += addingValue;
            LatLng pointsPos = new LatLng(pointLatitude, pointLongitude);
            customMapDetectionPoints.add(pointsPos);
        }


    }
}
 //The problem is here! 
 public void cancelMapMovement(CameraPosition camPos ) {
    //HOW CAN I STOP THE MOVEMENT OVER THE GROUND OVERLAY EDGE
    //And make sure that the user dosen't see over the edge
    globalMap.getUiSettings().setAllGesturesEnabled(false);
    globalMap.moveCamera(CameraUpdateFactory.newCameraPosition(camPos));

}

此时我认为我有两种可能的解决方案:

1-仅使用setLatLngBoundsForCameraTarget()功能并为相机设置偏移或边距。但它是否可能并且它是否适用于我的用例?

2-使用已编写的代码解决相机限制问题

感谢您的帮助!如果你找到一个,建议其他解决方案!

如果需要,我会提供更多信息。

0 个答案:

没有答案