Reputation: 3580
I want to zoom the camera based on the markers' distances. If all the markers present in the Google Maps cannot be entirely covered on a street level zoom(15) , then switch to another built-in zoom function: CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding)
, which satisfies the requirement. I refrain from using this built-in function when the markers are too close to each other or there's only one marker because it zooms on the maximum level. I have tried to use GoogleMap#setMaxZoomPreference
but it doesn't seem to work. The zoom level does not necessarily prohibits to clamp on a certain maximum value(which setting a max zoom level becomes void) but I need to set the initial zoom where the user can see all of the markers present in the entire map.
How do I measure the camera viewport in meters?
I will be needing the viewable area and gather all the distances of markers to determine if I will use the street level zoom level or the built-in zoom function.
Upvotes: 1
Views: 1316
Reputation: 18242
You can measure the viewport width and height on each camera change like this:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnCameraIdleListener {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnCameraIdleListener(this);
}
@Override
public void onCameraIdle() {
VisibleRegion viewPort = mMap.getProjection().getVisibleRegion();
double viewPortHeight = SphericalUtil.computeDistanceBetween(viewPort.nearLeft, viewPort.farLeft);
double viewPortWidth = SphericalUtil.computeDistanceBetween(viewPort.nearLeft, viewPort.nearRight);
}
}
I'm using the SphericalUtil.computeDistanceBetween
method from the Google Maps Android API Utility Library
Upvotes: 1