Skip to content

Instantly share code, notes, and snippets.

@longhotsummer
Last active November 1, 2019 17:29
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save longhotsummer/e8b1b195caf0a509f2e4 to your computer and use it in GitHub Desktop.
Save longhotsummer/e8b1b195caf0a509f2e4 to your computer and use it in GitHub Desktop.
Using leaflet.js to pan and zoom a big image - http://kempe.net/blog/2014/06/14/leaflet-pan-zoom-image.html
// Using leaflet.js to pan and zoom a big image.
// See also: http://kempe.net/blog/2014/06/14/leaflet-pan-zoom-image.html
// create the slippy map
var map = L.map('image-map', {
minZoom: 1,
maxZoom: 4,
center: [0, 0],
zoom: 1,
crs: L.CRS.Simple
});
// dimensions of the image
var w = 2000,
h = 1500,
url = 'http://kempe.net/images/newspaper-big.jpg';
// calculate the edges of the image, in coordinate space
var southWest = map.unproject([0, h], map.getMaxZoom()-1);
var northEast = map.unproject([w, 0], map.getMaxZoom()-1);
var bounds = new L.LatLngBounds(southWest, northEast);
// add the image overlay,
// so that it covers the entire map
L.imageOverlay(url, bounds).addTo(map);
// tell leaflet that the map is exactly as big as the image
map.setMaxBounds(bounds);
<style>
#image-map {
width: 100%;
height: 300px;
border: 1px solid #ccc;
margin-bottom: 10px;
}
</style>
<div id="image-map"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment