我正在使用Google地图API 2,在地图上显示多个标记。我的问题是,我希望我的相机位置位于所有标记上。 对于单个标记,我已经解决了这个问题。
myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MyLocation, 22));
我正在使用Google地图API 2,在地图上显示多个标记。我的问题是,我希望我的相机位置位于所有标记上。 对于单个标记,我已经解决了这个问题。
myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MyLocation, 22));
我用以下方法来查看视图中的所有标记。它会将您的Mapview限定在您的arraylist中包含的所有纬度和经度位置上。
// Pan to see all markers in view.
// Cannot zoom to bounds until the map has a size.
final View mapView = getSupportFragmentManager().findFragmentById(R.id.map).getView();
if (mapView.getViewTreeObserver().isAlive()) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@Override
public void onGlobalLayout() {
LatLngBounds.Builder bld = new LatLngBounds.Builder();
for (int i = 0; i < YOUR_ARRAYLIST.size(); i++) {
LatLng ll = new LatLng(YOUR_ARRAYLIST.get(i).getPos().getLat(), YOUR_ARRAYLIST.get(i).getPos().getLon());
bld.include(ll);
}
LatLngBounds bounds = bld.build();
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 70));
mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
希望它能帮到你。