我想设置地图缩放级别,以适应在地图上的所有标记。 我用下面的方法被很多人说,但它不是为我工作。 这是显示一些其他的点。
if (positions.size() > 0) {
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker m : positions) {
builder.include(m.getPosition());
}
builder.include(positions.get(i).getPosition());
}
try {
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
builder.build(), UserDataManager.getInstance().getWidth(),
UserDataManager.getInstance().getWidth(),0));
googleMap.setOnCameraChangeListener(null);
}
});
} catch (IllegalStateException e) {
// TODO: handle exception
final View mapView = getSupportFragmentManager()
.findFragmentById(R.id.map).getView();
if (mapView.getViewTreeObserver().isAlive()) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
// We check which build version we are using.
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
mapView.getViewTreeObserver()
.removeGlobalOnLayoutListener(
this);
} else {
mapView.getViewTreeObserver()
.removeOnGlobalLayoutListener(
this);
}
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
builder.build(),
UserDataManager.getInstance().getWidth(),
UserDataManager.getInstance().getWidth(),0));
googleMap.setOnCameraChangeListener(null);
}
});
}
});
}
}
}