That Leaflet plugin allows you to use a DIV as the active area. All positionning methods (setView, fitBounds, setZoom) will be applyed on this DIV instead of the all map. A typical use case is to set a map as background (like that example) and center it on the top of the screen for example. Defining media queries on that DIV make it easy to adapt the active area according client resolution.
Try the example page !
Include the javascript file :
<script src="L.activearea.js"></script>
Add a CSS class (".activeArea" in that example) with absolute position
.activeArea { position: absolute; top: 50px; left: 50px; right: 50px; height: 200px; }
And instanciate your map like that :
var map = new L.Map(document.createElement('div')).setActiveArea('activeArea');
You can also give an object with HTML style properties:
var map = new L.Map(document.createElement('div')).setActiveArea({
position: 'absolute',
top: '50px',
left: '50px',
right: '50px',
height: '200px'
});
Then, the map will center itself in the center of that DIV when calling setView, setZoom, fitBounds, etc.
If your need to re-center the map automatically, pass 'true' as second argument :
map.setActiveArea('activeArea', true, true);
Pass true as 3rd argument to animate the pan (default = false)
map.getBounds()returns the geographic bounds of the active area.map.getMapBounds()returns the geographic bounds of the entire map container, regardless of the active area (useful e.g. for filtering markers or objects on the entire visible map).
If you use React Leaflet, you can enable the active area from a child component of MapContainer using the useMap hook:
import React, { useEffect } from "react";
import { MapContainer, TileLayer, useMap } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import "leaflet-active-area";
function ActiveAreaSetup() {
const map = useMap();
useEffect(() => {
map.setActiveArea("activeArea", true, true);
}, [map]);
return null;
}
export default function MyMap() {
return (
<MapContainer
center={[48.853, 2.349]}
zoom={13}
style={{ height: "100vh", width: "100vw" }}
>
<ActiveAreaSetup />
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
</MapContainer>
);
}Then style .activeArea in your CSS with an absolute position, for example:
.activeArea {
position: absolute;
top: 50px;
left: 50px;
right: 50px;
height: 200px;
}To install the pre-commit hook (which launch tests): ln -s ../../pre-commit.sh .git/hooks/pre-commit
Thanks to all contributors : https://github.com/Mappy/Leaflet-active-area/graphs/contributors
This code is provided under the Apache 2.0 license.