Skip to content

Instantly share code, notes, and snippets.



Last active Jan 21, 2020
What would you like to do?
Map using leaflet.js and d3,js overlaid
license: mit

This is a map that uses leaflet.js and d3.js in a way that combines the two so that the d3.js object stays fixed in relation to a geographic point but remains the same size on the screen. It is as used as an example in the book D3 Tips and Tricks.

It is derived from the Pere Roca Ristol example but is simplified somewhat.

<!DOCTYPE html>
<title>d3.js with leaflet.js</title>
<script src=""></script>
<div id="map" style="width: 600px; height: 400px"></div>
<script type="text/javascript">
var map ='map').setView([-41.2858, 174.7868], 13);
mapLink =
'<a href="">OpenStreetMap</a>';
'http://{s}{z}/{x}/{y}.png', {
attribution: '&copy; ' + mapLink + ' Contributors',
maxZoom: 18,
/* Initialize the SVG layer */
/* We simply pick up the SVG from the map object */
var svg ="#map").select("svg"),
g = svg.append("g");
d3.json("circles.json", function(collection) {
/* Add a LatLng object to each item in the dataset */
collection.objects.forEach(function(d) {
d.LatLng = new L.LatLng([0],[1])
var feature = g.selectAll("circle")
.style("stroke", "black")
.style("opacity", .6)
.style("fill", "red")
.attr("r", 20);
map.on("viewreset", update);
function update() {
function(d) {
return "translate("+
map.latLngToLayerPoint(d.LatLng).x +","+
map.latLngToLayerPoint(d.LatLng).y +")";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment