LBes
LBes

Reputation: 3456

Make SVGs on top of leaflet map not respond to pan events

I have created this simple leaflet map example on top of which I draw svgs with the following code:

var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
    osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
    osm = L.tileLayer(osmUrl, {maxZoom: 20, attribution: osmAttrib});

var map = L.map('map').setView([37.5, -115], 6).addLayer(ism);


/* Initialize the SVG layer */
map._initPathRoot()    

/* We simply pick up the SVG from the map object */
var svg = d3.select("#map").select("svn")
g = svg.append("g")
    .attr("width",width)
    .attr("height", height);

var row = svg.selectAll(".row")
    .data(gridData)
    .enter().append("g")
    .attr("class", "row");

var column = row.selectAll(".square")
    .data(function(d) { return d; })
    .enter().append("rect")
    .attr("class","square")
    .attr("x", function(d) { return d.x; })
    .attr("y", function(d) { return d.y; })
    .attr("width", function(d) { return d.w; })
    .attr("height", function(d) { return d.h; })
    .style("fill", "#fff")
    .style("fill-opacity","0.1")
    .style("stroke", "#222")

Now my problem is that whenever I pan the map, whatever I added as SVG on the map is also panned, is there any way around that?

Upvotes: 0

Views: 200

Answers (1)

IvanSanchez
IvanSanchez

Reputation: 19069

You're putting all your D3 stuff inside a SVG container which is managed by Leaflet:

/* We simply pick up the SVG from the map object */
var svg = d3.select("#map").select("svn")

...and Leaflet takes care of moving and zooming around anything which is inside a map container.

is there any way around that?

Yes. Don't reuse Leaflet's <svg> root containers from L.SVG renderers for your own purposes.

Upvotes: 1

Related Questions