添加标记后,我使用以下代码在 map 上缩放至最大缩放级别。它可以工作,但前提是我让它在 500 毫秒的延迟运行时间内。如果取出来的话,就没有作用了。我知道我错过了一些明显的东西,但我不知道我错过了什么。谁能告诉我需要将 LatLngBounds 代码放在哪里才能使其在没有 Runnable 的情况下工作?
// Expand our Zoom to fit all markers
LatLngBounds.Builder builder = new LatLngBounds.Builder();
//the include method will calculate the min and max bound.
builder.include(myLocationPin.getPosition());
builder.include(placeLocationPin.getPosition());
final LatLngBounds bounds = builder.build();
final int zoomWidth = getResources().getDisplayMetrics().widthPixels;
final int zoomHeight = getResources().getDisplayMetrics().heightPixels;
final int zoomPadding = (int) (zoomWidth * 0.10); // offset from edges of the map 12% of screen
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,zoomWidth,zoomHeight,zoomPadding));
}
}, 500);
最佳答案
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_map);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
map.addMarker(new MarkerOptions().title("your title")
.snippet("your desc")
.position(new LatLng(-22.2323,-2.32323)));
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,zoomWidth,
zoomHeight,zoomPadding));
}
});
}
关于android - Google map Android LatLngBounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41168105/