Search code examples
androidoverlayzoomingandroid-mapviewitemizedoverlay

Zoom into group of points in the MapActivity (ItemizedOverlay) in Android


If I want all my POIs to be visible in the Map, then I need to adjust two parameters dynamically:

  • Center-Focus of the Map
  • Zoom step

I am wondering if there is such a behavior already built into the MapActivity? If not, could you provide me sample code?


Solution

  • The answer is not to find the centre point amongst a group of points and then call mapController.zoomToSpan(centrePoint). Instead, do the following in your ItemizedOverlay:

    public void calculateMapBounds()
    {       
        int minLat = Integer.MAX_VALUE;
        int minLon = Integer.MAX_VALUE;
        int maxLat = Integer.MIN_VALUE;
        int maxLon = Integer.MIN_VALUE;
    
        for (LocatorPosition position : mPositions)
        {
            minLat = Math.min(position.getLatitudeE6(), minLat);
            minLon = Math.min(position.getLongitudeE6(), minLon); 
            maxLat = Math.max(position.getLatitudeE6(), maxLat); 
            maxLon = Math.max(position.getLongitudeE6(), maxLon);
        }
    
        spanLat = maxLat - minLat;
        spanLon = maxLon - minLon;
    
    }
    

    Then call mapController.zoomToSpan(spanLat, spanLon);