7

I have a cluster marker that defines a bounding rectangle containing a group of markers. The cluster has a center (lat,lon), a marker count and a latitude and longitude span to calculate the bounds.

The Google Maps API (JS) has a function called "getBoundsZoomLevel(bounds)" but I cannot find an equivalent method in the Google Maps SDK for Android. How can I estimate the zoom level for given bounds (especially on different devices with different resolutions/densitys)?

I've planned that an user can touch a cluster marker and the map view will be centered to the center and the bounds of that cluster.

Has anybody a working code snippet or some suggestions for me?

Thanks in advance Regards Thorsten.

Amro
  • 123,847
  • 25
  • 243
  • 454
Thorsten
  • 71
  • 1
  • 3

3 Answers3

11

Thank you for your answer Solvek. Meanwhile I found a solution that I've adapted slightly and it works. The method computes the bounds of a set of geo points, computes the span of these points and finally uses zoomToSpan and animateTo for zooming into and centering the given area:

 public static void zoomInBounds(final MapView view, final GeoPoint.. bounds) {

    int minLat = Integer.MAX_VALUE;
    int minLong = Integer.MAX_VALUE;
    int maxLat = Integer.MIN_VALUE;
    int maxLong = Integer.MIN_VALUE;

    for (GeoPoint point : bounds) {
        minLat = Math.min(point.getLatitudeE6(), minLat);
        minLong = Math.min(point.getLongitudeE6(), minLong);
        maxLat = Math.max(point.getLatitudeE6(), maxLat);
        maxLong = Math.max(point.getLongitudeE6(), maxLong);
    }

    final MapController controller = view.getController();
    controller.zoomToSpan(
                       Math.abs(minLat - maxLat), Math.abs(minLong - maxLong));
    controller.animateTo(new GeoPoint((maxLat + minLat) / 2,
        (maxLong + minLong) / 2));
}
user640688
  • 321
  • 3
  • 10
  • Thank you for the answer. But sometimes zoomToSpan does not work well. There are instances when some of my pin marker are out of bounds in the screen. Do you run into the same issue? – Ahmed Faisal Sep 21 '11 at 14:39
  • yes, see the docs "Attempts to adjust the zoom of the map so that the given span of latitude and longitude will be displayed. 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." For my purposes I just added say 20% to the bounds to effectivly have some padding – Dori Jan 18 '12 at 15:35
3

Now you could zoom to bounds with .newLatLngBounds() method:

map.animateCamera(CameraUpdateFactory.newLatLngBounds(place.getViewport(), 10)); // 10 is padding

It will move and zoom to given bounds, so they all visible on a screen (e.g. different zoom for city or country). It works perfectly with location search.

0

I didn't tried this but maybe it will work for you. Imagine you have a rectangle (x1,y1)-(x2,y2)

    MapController c = mapView.getController();
    c.setCenter(new GeoPoint((x1+x2)/2,(y1+y2)/2));
    c.zoomToSpan(x2, y2);
Solvek
  • 5,158
  • 5
  • 41
  • 64