1

I have a mapView with several points plotted with markers, each with different latitudes and longitudes that may or may not be near each other in vicinity. Is there a way I can have the MapView open to the deepest zoom level that centers all the points on the screen together?

I know the mapController has a setZoom and setCenter property, but how can I determine the values for those methods? Or is there another simpler way to accomplish this?

ant-depalma
  • 2,006
  • 4
  • 26
  • 34
  • This was solved here: [Stack Overflow][1] [1]: http://stackoverflow.com/questions/5241487/android-mapview-setting-zoom-automatically-until-all-itemizedoverlays-are-visi – ant-depalma Jul 16 '12 at 14:15

1 Answers1

5

In your MapView, after you have added your overlayItems in your ImtemizedOverlay, like this:

GeoPoint aGeoPoint = new GeoPoint((int)(latitude * 1E6), (int)(longitude * 1E6));
OverlayItem aOverlayItem = new OverlayItem(aGeoPoint, "Auto", "Auto");
locationsItemizedOverlay.addOverlay(aOverlayItem);

Try this:

    // Center map in geopoints
    GeoPoint point = locationsItemizedOverlay.getCenterPoint();

    int latSpan = locationsItemizedOverlay.getLatSpanE6();
    int longSpan = locationsItemizedOverlay.getLonSpanE6();

    mapView.getController().setCenter(point);
    mapView.getController().zoomToSpan((int)(latSpan * Constants.MAP_ZOOM_FACTOR), (int)(longSpan * Constants.MAP_ZOOM_FACTOR));

In your overriden ItemizedOverlay:

public GeoPoint getCenterPoint() {
    if(center == null) {
        int northEdge = -90000000; // i.e., -90E6 microdegrees
        int southEdge = 90000000;
        int eastEdge = -180000000;
        int westEdge = 180000000;

        Iterator<OverlayItem> iterator = overlayItemsArrayList.iterator();
        while(iterator.hasNext()) {
            GeoPoint geoPoint = iterator.next().getPoint();

            if(geoPoint.getLatitudeE6() > northEdge)
                northEdge = geoPoint.getLatitudeE6();

            if(geoPoint.getLatitudeE6() < southEdge)
                southEdge = geoPoint.getLatitudeE6();

            if(geoPoint.getLongitudeE6() > eastEdge)
                eastEdge = geoPoint.getLongitudeE6();

            if(geoPoint.getLongitudeE6() < westEdge)
                westEdge = geoPoint.getLongitudeE6();
        }

        center = new GeoPoint((int)((northEdge + southEdge) / 2), (int)((westEdge + eastEdge) / 2));
    }

    return center;
}

I hope it helps.

Jorge Gil
  • 4,265
  • 5
  • 38
  • 57