Home » Android » Auto-size zoom on Google Maps in java? (depending android screen resolution)

Auto-size zoom on Google Maps in java? (depending android screen resolution)

Posted by: admin June 15, 2020 Leave a comment


I’ve got 2 GeoPoints given to show them on the map with markers.

How can I get the optimum zoom level for the MapController in order to focus the middle of both points, but also have them on the map.

The whole thing should work at different screen resolutions.

How to&Answers:

You can do this using the MapView’s MapController. You get the MapController using MapView.getController(), then you can attempt to set the correct zoom using MapController.zoomToSpan(). I also tend to use ItemizedOverlay’s getLatSpanE6() and getLonSpanE6() to make this simpler. An example:

MapView map = (MapView) findViewById(R.id.Map);
MapController mc = map.getController();
mc.zoomToSpan(overlay.getLatSpanE6(), overlay.getLonSpanE6());

It is important to note their caveat:

Because the zoom can only achieve
discrete levels, and because the
aspect ratio of the map may not match
the ratio given, the quality of the
fit may vary. The only thing we
guarantee is that, after the zoom, at
least one of the new latitude or the
new longitude will be within a factor
of 2 from the corresponding parameter.

Edit: In order to also get the map to zoom on the correct place, you have to use MapController.setCenter().


This is how it should be done:

private void loadMarkerPositions() {

    for (Stores store : storeList) {
        latlong1 = new LatLng(store.getLatitude(), store.getLongtitude());

     List<LatLng> points = array; 

     final LatLngBounds.Builder bc = new LatLngBounds.Builder();

        for (LatLng item : points) {

    googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {

        public void onCameraChange(CameraPosition arg0) {
            // Move camera.
            googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bc.build(), 30));
            // Remove listener to prevent position reset on camera move.


Reference: moveCamera with CameraUpdateFactory.newLatLngBounds crashes

There is another easy way, you can use this project to accomplish all map related activities: