Skip to content

Instantly share code, notes, and snippets.

@johnwalley
Last active December 5, 2023 13:49
Show Gist options
  • Save johnwalley/371800492a010817c0795976e8a172c7 to your computer and use it in GitHub Desktop.
Save johnwalley/371800492a010817c0795976e8a172c7 to your computer and use it in GitHub Desktop.
Cambridge Pub Map
license: bsd-3-clause

Example of adding an d3 generated SVG overlay to a Leaflet map

Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<!DOCTYPE html>
<html>
<title>Cambridge Pub Map</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ=="
crossorigin="" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha512-/Nsx9X4HebavoBvEBuyp3I7od5tA0UzAxs+j83KgC8PU0kgB4XiK4Lfe4y4cgBtaRJQEIFCW+oC506aPT2L1zw=="
crossorigin=""></script>
<style>
body {
padding: 0;
margin: 0;
}
html,
body,
#map {
height: 100%;
width: 100%
}
.leaflet-container {
background-color: rgba(0, 0, 0, 0);
}
</style>
<div id="map"></div>
<script>
const width = 1600;
const height = 1200;
const initialZoomLevel = 1;
const imageUrl = 'cambridge-pub-map.svg';
const imageBounds = [[0, 0], [height, width]];
const map = new L.map('map', {
crs: L.CRS.Simple,
center: [height * 9 / 11, width / 4],
zoom: initialZoomLevel,
attributionControl: false,
maxBounds: [[0, 0], [height, width]],
})
L.imageOverlay(imageUrl, imageBounds, { interactive: true }).addTo(map);
map.on('click', (e) => console.log(e));
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment