Zooming and Centering

Zooming and centering are essential map features that allow users to adjust the zoom level and focus on specific geographical locations within the map view.

The Maps add-on provides methods that developers can use to programmatically control these features.

Let’s explore some methods of the GeoMap class:

  • setCenter() - sets the central geographic point within the visible area of the map. The coordinates should be provided in the map projection, specifically in EPSG:4326. In case the map projection has been altered using setProjection(CRS), the coordinates must then be specified in that adjusted projection. It’s important to differentiate the map projection from the view projection configured in the map’s GeoMapView. The view projection influences how map data is understood and displayed, whereas the map projection sets the coordinate system for all coordinates exchanged with the public API.

  • setZoom(double zoom) - adjusts the map to a particular zoom level. The zoom level begins at 0, representing the maximum zoomed-out view, and increases as you zoom in for a closer perspective

  • zoomToExtent(Extent) - sets the visible area of the map to encompass the provided geographical boundaries at the highest possible zoom level. Extent is a bounding box that defines a specific geographical area on the map. The extent is represented by a set of coordinates: the minimum (left, bottom) and maximum (right, top) coordinates that delineate the boundaries of a rectangular region on the map. For a more advanced method of zooming to a specific extent, use the fit(FitOptions) method.

  • zoomToGeometry(SimpleGeometry) - sets the visible area of a map to encompass the envelope of the provided geometry at the maximum achievable zoom level.

    If the provided geometry is a Point, the map will be centered on this point at the current zoom level.

    A more advanced method for zooming to a specific geometry can be achieved by using the fit(FitOptions) method.

  • zoomToFeature(Feature) - a utility method that extracts the geometry from the provided feature and delegates to zoomToGeometry(SimpleGeometry).

  • fit(FitOptions) - adjusts the provided geometry or extent according to the specified map size, border settings, and specified options. For example, to set a maximum zoom level and enable animation, the following approach can be taken:

    protected GeometryFactory geometryFactory = GeometryUtils.getGeometryFactory();
    
    @ViewComponent
    private GeoMap map;
    
    @Subscribe("map")
    public void onMapMapSingleClick(final MapSingleClickEvent event) {
        map.fit(new FitOptions(new PointFeature(
                geometryFactory.createPoint(event.getCoordinate())))
                .withDuration(2000)
                .withEasing(Easing.LINEAR)
                .withMaxZoom(20d));
    }

    FitOptions are used to configure how a map’s view should fit or adjust to a specific extent or geometry. These options provide developers with control over aspects such as zoom level, duration of animation, easing functions, and other parameters related to fitting the map view.

    For additional information, refer to the documentation for View.