Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Map Zoom to Show all Pins

I have 5 pins added to a map. How can I tell the MapView to zoom as much as possible and keep all pins in view?

like image 724
Ian Vink Avatar asked Aug 26 '10 15:08

Ian Vink


2 Answers

this is much easier solution if you are using an ItemizedOverlay

    public void fitOverlays() {
        mMapView.getController().zoomToSpan(mItemizedOverlay.getLatSpanE6(), mItemizedOverlay.getLonSpanE6());
}
like image 111
Nathan Schwermann Avatar answered Sep 29 '22 16:09

Nathan Schwermann


I used this method in a recent project of mine

public void centerOverlays() {
    int minLat = 81 * MapStoresController.MAP_SCALE;
    int maxLat = -81 * MapStoresController.MAP_SCALE;
    int minLon = 181 * MapStoresController.MAP_SCALE;
    int maxLon = -181 * MapStoresController.MAP_SCALE;

    for (int i = 0; i < overlayItems.size(); i++) {
        Store s = overlayItems.getItem(i).getStore();
        minLat = (int) ((minLat > (s.getLocation().getLatitude() * MapStoresController.MAP_SCALE)) ? s.getLocation().getLatitude() * MapStoresController.MAP_SCALE :    minLat);
        maxLat = (int) ((maxLat < (s.getLocation().getLatitude() * MapStoresController.MAP_SCALE)) ? s.getLocation().getLatitude() * MapStoresController.MAP_SCALE : maxLat);
        minLon = (int) ((minLon > (s.getLocation().getLongitude() * MapStoresController.MAP_SCALE)) ? s.getLocation().getLongitude() * MapStoresController.MAP_SCALE : minLon);
        maxLon = (int) ((maxLon < (s.getLocation().getLongitude() * MapStoresController.MAP_SCALE)) ? .getLocation().getLongitude() * MapStoresController.MAP_SCALE : maxLon);
    }

    GeoPoint gp = controller.getUserLocation();

    minLat = (minLat > gp.getLatitudeE6()) ? gp.getLatitudeE6() : minLat;
    maxLat = (maxLat < gp.getLatitudeE6()) ? gp.getLatitudeE6() : maxLat;
    minLon = (minLon > gp.getLongitudeE6()) ? gp.getLongitudeE6() : minLon;
    maxLon = (maxLon < gp.getLongitudeE6()) ? gp.getLongitudeE6() : maxLon;

    mapView.getController().zoomToSpan((maxLat - minLat), (maxLon - minLon));
    mapView.getController().animateTo(new GeoPoint((maxLat + minLat) / 2, (maxLon + minLon) / 2));
}

Basically it just finds the bounds of a box and sets the view to the closest one that encompasses the bounds

like image 36
Andrew Burgess Avatar answered Sep 29 '22 17:09

Andrew Burgess