在谷歌地图android api v2中设置最大缩放级别

在谷歌地图android api v2中设置最大缩放级别,android,maps,zooming,Android,Maps,Zooming,我目前正在使用谷歌地图android API v2开发应用程序。 我的代码如下。 假设地图有多个标记并放大以显示显示中的所有标记 LatLngBuilder.Builder builder = LatLngBounds.builder(); for(Marker m : markers){ builder.include(m.getPosition()); } LatLngBounds bounds = builder.build(); map.animateCamera(CameraU

我目前正在使用谷歌地图android API v2开发应用程序。 我的代码如下。 假设地图有多个标记并放大以显示显示中的所有标记

LatLngBuilder.Builder builder = LatLngBounds.builder();
for(Marker m : markers){
    builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10);
这段代码运行良好,但我想停止动画时,缩放水平达到17.0f; MAPAPI似乎没有这种方法来控制缩放级别。
有人知道解决这个问题的方法吗?

地图有一个叫做maxZoom的属性。创建地图时,只需将其设置为您的值

我不确定这是否可行,但您能试试吗

map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

希望它有帮助

您可以通过调用它来获取最大缩放级别
getMaxZoomLevel()
,然后设置该值:

mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(fromPosition, getMaxZoomLevel()));

我还没有在谷歌地图API中找到任何直接的解决方案。此问题的一个潜在解决方案是侦听:每当此事件触发且缩放级别高于最大缩放级别时,可以调用。产生的代码如下所示:

@Override
public void onCameraChange(CameraPosition position) {
    float maxZoom = 17.0f;
    if (position.zoom > maxZoom)
        map_.animateCamera(CameraUpdateFactory.zoomTo(maxZoom));
}
这是您正在使用的
include(position)
的源代码:

“包含此点以建立边界。边界将以最小方式扩展以包含此点。”。 更确切地说,它将考虑在东、西两个方向上延伸边界(其中一个可以环绕世界),并选择两个较小的边界。在两个方向产生相同大小的LangngIn的情况下,这将使其向东延伸。 地图将缩小,直到可以显示要添加到for循环中的所有标记

如果只想缩小到17级并仍显示标记,请先设置动画以缩放到17级,然后获取其边界,然后添加标记

@Override
public void onCameraChange(CameraPosition camPos) {

    if (camPos.zoom < 17 && mCurrentLoc != null) {
        // set zoom 17 and disable zoom gestures so map can't be zoomed out
        // all the way
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position,17));
        mMap.getUiSettings().setZoomGesturesEnabled(false);
    }
    if (camPos.zoom >= 17) {
        mMap.getUiSettings().setZoomGesturesEnabled(true);
    }

    LatLngBounds visibleBounds =  mMap.getProjection().getVisibleRegion().latLngBounds;

//add the markers
@覆盖
CameraChange(CameraPosition camPos)上的公共空间{
如果(camPos.zoom<17&&mCurrentLoc!=null){
//设置zoom 17并禁用缩放手势,使地图无法缩小
//一路
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(位置17));
mMap.getUiSettings().setZoomGeturesEnabled(false);
}
如果(camPos.zoom>=17){
mMap.getUiSettings().setZoomGeturesEnabled(true);
}
LatLngBounds visibleBounds=mMap.getProjection().getVisibleRegion().LatLngBounds;
//添加标记

在设置摄影机动画之前,您可以检查边界的SW和NE点是否太近,如有必要,可以调整边界:

        ...
        LatLngBounds bounds = builder.Build();

        var sw = bounds.Southwest;
        var ne = bounds.Northeast;
        var deltaLat = Math.Abs(sw.Latitude - ne.Latitude);
        var deltaLon = Math.Abs(sw.Longitude - ne.Longitude);

        const double zoomN = 0.005; // set whatever zoom coefficient you need!!!
        if (deltaLat < zoomN) {
            sw.Latitude = sw.Latitude - (zoomN - deltaLat / 2);
            ne.Latitude = ne.Latitude + (zoomN - deltaLat / 2);
            bounds = new LatLngBounds(sw, ne);
        }
        else if (deltaLon < zoomN) {
            sw.Longitude = sw.Longitude - (zoomN - deltaLon / 2);
            ne.Longitude = ne.Longitude + (zoomN - deltaLon / 2);
            bounds = new LatLngBounds(sw, ne);
        }

        map.animateCamera(CameraUpdateFactory.NewLatLngBounds(bounds, 10);
。。。
LatLngBounds bounds=builder.Build();
var sw=边界。西南;
var ne=边界。东北;
var deltaLat=数学绝对值(西南纬度-东北纬度);
var deltaLon=Math.Abs(西南经度-东北经度);
const double zoomN=0.005;//设置所需的缩放系数!!!
如果(deltaLat

注:我的例子是c#中的Xamarin,但您可以轻松地为java调整它

以下是Arvis解决方案的相应java代码,对我来说效果很好:

private LatLngBounds adjustBoundsForMaxZoomLevel(LatLngBounds bounds) {
  LatLng sw = bounds.southwest;
  LatLng ne = bounds.northeast;
  double deltaLat = Math.abs(sw.latitude - ne.latitude);
  double deltaLon = Math.abs(sw.longitude - ne.longitude);

  final double zoomN = 0.005; // minimum zoom coefficient
  if (deltaLat < zoomN) {
     sw = new LatLng(sw.latitude - (zoomN - deltaLat / 2), sw.longitude);
     ne = new LatLng(ne.latitude + (zoomN - deltaLat / 2), ne.longitude);
     bounds = new LatLngBounds(sw, ne);
  }
  else if (deltaLon < zoomN) {
     sw = new LatLng(sw.latitude, sw.longitude - (zoomN - deltaLon / 2));
     ne = new LatLng(ne.latitude, ne.longitude + (zoomN - deltaLon / 2));
     bounds = new LatLngBounds(sw, ne);
  }

  return bounds;
}
private LatLngBounds adjustbounds formaxzoomlevel(LatLngBounds边界){
LatLng西南=边界西南;
LatLng ne=边界。东北方向;
双三角=数学绝对值(西南纬度-东北纬度);
double deltaLon=Math.abs(西南经度-东北经度);
最终双缩放=0.005;//最小缩放系数
如果(deltaLat
@kasimir的方法设置了纬度或经度的最小度数,感觉有点难读。所以我调整了它,只设置了纬度的最小度数,我觉得这样更容易阅读:

private LatLngBounds adjustBoundsForMinimumLatitudeDegrees(LatLngBounds bounds, double minLatitudeDegrees) {
    LatLng sw = bounds.southwest;
    LatLng ne = bounds.northeast;
    double visibleLatitudeDegrees = Math.abs(sw.latitude - ne.latitude);

    if (visibleLatitudeDegrees < minLatitudeDegrees) {
        LatLng center = bounds.getCenter();
        sw = new LatLng(center.latitude - (minLatitudeDegrees / 2), sw.longitude);
        ne = new LatLng(center.latitude + (minLatitudeDegrees / 2), ne.longitude);
        bounds = new LatLngBounds(sw, ne);
    }

    return bounds;
}
private LatLngBounds AdjustBoundsForminimumlatitedegrees(LatLngBounds bounds bounds,double minLatitudeDegrees){
LatLng西南=边界西南;
LatLng ne=边界。东北方向;
双可见纬度梯度=数学绝对值(西南纬度-东北纬度);
if(可视的相对标准<最小相对标准){
LatLng center=bounds.getCenter();
sw=新纬度(中心纬度-(最小纬度度/2),西南经度);
ne=新纬度(中心纬度+(最小纬度度/2),东北经度);
边界=新的LatLngBounds(西南、东北);
}
返回边界;
}

我最后做的是创建自己的按钮并禁用默认按钮,这样我就可以完全控制了

在布局中放置按钮的方式如下:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="bottom"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true">

    <Button
        android:id="@+id/bzoomin"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:gravity="center"
        android:textSize="28sp"
        android:text="+" />
    <Button
            android:id="@+id/bzoomout"
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:gravity="center"
            android:textSize="23sp"
            android:text="—" />
</LinearLayout>

然后在代码中输入以下命令以禁用默认按钮并设置新按钮:

map.getUiSettings().setZoomControlsEnabled(false);

// setup zoom control buttons
Button zoomout = (Button) findViewById(R.id.bzoomout);
zoomout.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
        if(map.getCameraPosition().zoom >= 8.0f){
            // Zoom like normal
            map.animateCamera(CameraUpdateFactory.zoomOut());
        }else{
            // Do whatever you want if user went too far
            Messages.toast_short(MapsActivity.this, "Maximum zoom out level reached");
        }
    }

});

Button zoomin = (Button) findViewById(R.id.bzoomin);
zoomin.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (map.getCameraPosition().zoom <= 14.0f) {
            // Zoom like normal
            map.animateCamera(CameraUpdateFactory.zoomIn());
        } else {
            // Do whatever you want if user went too far
            Messages.toast_short(MapsActivity.this, "Maximum zoom in level reached");
        }
    }
});
map.getUiSettings().setZoomControlsEnabled(false);
//设置缩放控制按钮
按钮zoomout=(按钮)findViewById(R.id.bzoomout);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(map.getCameraPosition().zoom>=8.0f){
//像平常一样变焦
animateCamera(CameraUpdateFactory.zoomOut());
}否则{
//如果用户走得太远,可以做任何您想做的事情
Messages.toast_short(mapsacativity.this,“达到最大缩小级别”);
}
}
});
按钮缩放=(按钮)findViewById
@Override
public void zoomToMarkers(Set<Marker> markers) {

    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    for (Marker marker : markers) {
        builder.include(marker.getPosition());
    }
    LatLngBounds bounds = builder.build();

    // Calculate distance between northeast and southwest
    float[] results = new float[1];
    android.location.Location.distanceBetween(bounds.northeast.latitude, bounds.northeast.longitude,
            bounds.southwest.latitude, bounds.southwest.longitude, results);

    CameraUpdate cu = null;
    if (results[0] < 1000) { // distance is less than 1 km -> set to zoom level 15
        cu = CameraUpdateFactory.newLatLngZoom(bounds.getCenter(), 15);
    } else {
        int padding = 50; // offset from edges of the map in pixels
        cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
    }
    if (cu != null) {
        mMap.moveCamera(cu);
    }
}
GoogleMap.setMaxZoomPreference()

GoogleMap.setMinZoomPreference()
private GoogleMap mMap;
// Set a preference for minimum and maximum zoom.
mMap.setMinZoomPreference(6.0f);
mMap.setMaxZoomPreference(14.0f);
final float maxZoom = 10.0f;

@Override
public void onCameraChange(CameraPosition position) {

    if (position.zoom > maxZoom)
        map.animateCamera(CameraUpdateFactory.zoomTo(maxZoom));
}
    googleMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
        @Override
        public void onCameraMove() {
            Log.d(App.TAG, "onCameraMove");

            CameraPosition position = googleMap.getCameraPosition();

            float maxZoom = 9.0f;
            if (position.zoom > maxZoom) {

                googleMap.setMinZoomPreference(maxZoom);
            }

            Log.d(App.TAG, "position.zoom = " + position.zoom);

        }
    });
map.setMinZoomPreference(floatValue);
1f: World
5f: Landmass/continent
10f: City
15f: Streets
20f: Buildings
val setMin = 10f
val setMax = 20f

googleMap.setMaxZoomPreference(setMax)
googleMap.setMinZoomPreference(setMin)