I want to place the zoom control in middle right of the the map i.e. in the middle of the right most side of the map. I have found solution to put the zoom control in different corners using the following code
var map = new L.map("map-container",{ zoomControl: false }); new L.Control.Zoom({ position: 'topleft' }).addTo(map);
So the positions can be
topleft topright bottomleft bottomright
But my goal is to put the control window in the middle right. Or even I put the control in the corner I want to add some margin to the top. How can I do that? Is there any idea?
In react You can get zoomLevel by using getZoom method() and using useRef.
Zoom − By default, this control exists at the top left corner of the map. It has two buttons "+" and "–", using which you can zoom-in or zoom-out the map. You can remove the default zoom control by setting the zoomControl option of the map options to false.
Controlling the zoom. A leaflet map has several ways to control the zoom level shown, but the most obvious one is setZoom(). For example, map.setZoom(0); will set the zoom level of map to 0.
Next Page Leaflet provides various controls such as zoom, attribution, scale, etc., where − Zoom − By default, this control exists at the top left corner of the map. It has two buttons "+" and "–", using which you can zoom-in or zoom-out the map.
And to center vertically, just override the class .leaflet-top to this: .leaflet-top { top: 50%; transform: translate (0%, -50%); } NOTE: These changes will affect other controls on the map such as zoom controls.
[0:29] To set the position of the ZoomControl, we can set the position to bottomright. If you look at the map, it's now on the bottom right. With the ZoomControl, we can also set the text, so let's set the zoomInText to this monocle emoji and how about the zoomOutText to a map emoji?
We can create additional Control placeholder(s), besides the 4 provided corners by default.
A nice advantage is that it allows putting several Controls in one of those placeholders. They will stack without overlapping, as in the standard corners.
JavaScript:
// Create additional Control placeholders function addControlPlaceholders(map) { var corners = map._controlCorners, l = 'leaflet-', container = map._controlContainer; function createCorner(vSide, hSide) { var className = l + vSide + ' ' + l + hSide; corners[vSide + hSide] = L.DomUtil.create('div', className, container); } createCorner('verticalcenter', 'left'); createCorner('verticalcenter', 'right'); } addControlPlaceholders(map); // Change the position of the Zoom Control to a newly created placeholder. map.zoomControl.setPosition('verticalcenterright'); // You can also put other controls in the same placeholder. L.control.scale({position: 'verticalcenterright'}).addTo(map);
Then it becomes easy styling those placeholders with CSS, because their DOM parent is the map container itself. Hence top
, bottom
, left
and right
can be specified with percentages (which use the parent's dimensions).
CSS:
.leaflet-verticalcenter { position: absolute; z-index: 1000; pointer-events: none; top: 50%; /* possible because the placeholder's parent is the map */ transform: translateY(-50%); /* using the CSS3 Transform technique */ padding-top: 10px; } .leaflet-verticalcenter .leaflet-control { margin-bottom: 10px; }
As for vertical centering the placeholder itself, you can use your favourite technique. Here I used the CSS3 Transform to offset the placeholder by half of its own height.
If necessary (e.g. for old browsers compatibility), you can rather use a "calculate-on-load" method to perform this offset, similar to iH8's answer. But you no longer need to run it on map resize, only when adding new Control(s) to the placeholder.
Live demo: https://plnkr.co/edit/bHJwfm598d1Ps7MpLG0k?p=preview
Note: there is currently an open PR (Leaflet/Leaflet #5554) for this, but since it is not compatible with old versions of Internet Explorer, it will not likely be merged in Leaflet core.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With