public void zoomToBestFit ( Set < GeoPosition > positions , double maxFraction ) { if ( positions . isEmpty ( ) ) return ; if ( maxFraction <= 0 || maxFraction > 1 ) throw new IllegalArgumentException ( "maxFraction must be between 0 and 1" ) ; TileFactory tileFactory = getTileFactory ( ) ; TileFactoryInfo info = tileFactory . getInfo ( ) ; if ( info == null ) return ; // set to central position initially GeoPosition centre = computeGeoCenter ( positions ) ; setCenterPosition ( centre ) ; if ( positions . size ( ) == 1 ) return ; // repeatedly zoom in until we find the first zoom level where either the width or height // of the points takes up more than the max fraction of the viewport // start with zoomed out at maximum int bestZoom = info . getMaximumZoomLevel ( ) ; Rectangle2D viewport = getViewportBounds ( ) ; Rectangle2D bounds = generateBoundingRect ( positions , bestZoom ) ; // is this zoom still OK? while ( bestZoom >= info . getMinimumZoomLevel ( ) && bounds . getWidth ( ) < viewport . getWidth ( ) * maxFraction && bounds . getHeight ( ) < viewport . getHeight ( ) * maxFraction ) { bestZoom -- ; bounds = generateBoundingRect ( positions , bestZoom ) ; } setZoom ( bestZoom + 1 ) ; }
Zoom and center the map to a best fit around the input GeoPositions . Best fit is defined as the most zoomed - in possible view where both the width and height of a bounding box around the positions take up no more than maxFraction of the viewport width or height respectively .