首页 > 解决方案 > JAVA Google Maps API:根据缩放值设置边界

问题描述

每次缩放值更改时,我都会尝试更新相机边界。检查文档后,我发现可以通过使用LatLngBounds Including(LatLng point)返回包含 LatLng 点的最小 LatLngBounds 的方法来扩展当前边界。所以我做了这个代码:

// I save the initial zoom value and I test if it changes
double zoom = mMap.getCameraPosition().zoom;
        if (zoom!= mMap.getCameraPosition().zoom){
// I create a new Bounds which is proportional to the difference of the previous and the new zoom value by including a new point in the boundaries
            LatLng chgtBounds = new LatLng(ParisBounds.getCenter().latitude+(mMap.getCameraPosition().zoom-zoom)*0.001,ParisBounds.getCenter().longitude+(mMap.getCameraPosition().zoom-zoom)*0.001);
            zoom = mMap.getCameraPosition().zoom;
// I apply this new bounds on the map and it automatically delete the previous one
            mMap.setLatLngBoundsForCameraTarget(ParisBounds.including(chgtBounds));
         }

我创建了一个新变量来保存缩放值,以了解用户何时放大地图。当他这样做时,通过将前一个边界增加缩放值的千分之一来创建一个新的边界(不确定这是正确的分数,但想法是相同的)。但是,我认为这些行只在应用程序启动时执行一次。有没有办法永远执行它们?我试过了while(true),但应用程序根本没有启动。

标签: javaandroid-studiogoogle-maps

解决方案


当前缩放级别来自:

mMap.getCameraPosition().zoom

请参阅文档

最大缩放(在 OP 代码中使用)是基于当前相机位置和使用的瓦片类型的最大可能缩放。


推荐阅读