Skip to content

Instantly share code, notes, and snippets.

@tomplex
Last active March 11, 2020 20:59
Show Gist options
  • Save tomplex/d9c87527f74dba90f3a90b1838c680cb to your computer and use it in GitHub Desktop.
Save tomplex/d9c87527f74dba90f3a90b1838c680cb to your computer and use it in GitHub Desktop.
Unexpected behavior with Leaflet map.setZoom
<!DOCTYPE html>
<html lang="en" xmlns="">
<head>
<meta charset="UTF-8">
<title>Zoom Weirdness</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"></script>
<style>
#map {
height: 500px;
width: 500px;
}
</style>
</head>
<body>
<div>
<button id="zoomit" type="button" style="top: 0">Zoom It</button>
</div>
<div id="map"></div>
</body>
<script>
let map = L.map('map').setView([51.505, -0.09], 13);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);
let circle = L.circle([51.508, -0.11], {
color: 'red',
radius: 500
}).addTo(map);
document.getElementById("zoomit").addEventListener('click', function () {
map.fitBounds(circle.getBounds());
map.setZoom(14);
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment