由于希望map可以自动设置自动的scale,使得屏幕伸缩根据marker的数量和位置自动的调节scale程度。
我们需要使用的是:LatLngBounds.Builder
根据visible的marker刷新map的代码如下:
private static LatLngBounds.Builder builder;
public static void refreshMap() {
builder = new LatLngBounds.Builder();
Set<Marker> markers = 你需要显示的marker集合
for (Marker marker : markers) {
if (marker.isVisible()) {
builder.include(marker.getPosition());
}
}
googleMap.setOnMapLoadedCallback(new OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
if (builder != null) {
LatLngBounds bounds = builder.build();
int padding = 0;
// offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(
bounds, padding);
googleMap.moveCamera(cu);
// Zoom in, animating the camera.
googleMap.animateCamera(cu);
}
}
});
}