我创建了这个简单的传单 map 示例,在其上使用以下代码绘制 svgs:
var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
osmAttrib = '© <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")
现在我的问题是,每当我平移 map 时,我在 map 上添加为 SVG 的任何内容也会被平移,有什么办法解决这个问题吗?
最佳答案
您将所有 D3 内容放入由 Leaflet 管理的 SVG 容器中:
/* We simply pick up the SVG from the map object */
var svg = d3.select("#map").select("svn")
...Leaflet 负责移动和缩放 map 容器内的任何内容。
is there any way around that?
是的。不要重复使用传单的<svg>
来自 L.SVG
的根容器用于您自己目的的渲染器。
关于javascript - 使传单 map 顶部的 SVG 不响应平移事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50787719/