javascript - 使传单 map 顶部的 SVG 不响应平移事件

标签 javascript d3.js leaflet

我创建了这个简单的传单 map 示例,在其上使用以下代码绘制 svgs:

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")

现在我的问题是,每当我平移 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/

相关文章:

javascript - 为什么 React 不更新 DOM?

javascript - 数据更改时 D3.js 表不刷新

java - 在 Java 上从 Rhino 启动 jsdom

javascript - 如何在传单js中获取缩放事件的经纬度

icons - Leaflet.js api 图标 : why do iconAnchor and popupAnchor have different coordinate locations

javascript - 当我启用 CSS 动画时,jQuery hide().slideDown() 不起作用

javascript - 如何使用 json 进行搜索并以 html 格式在搜索结果中显示选定的元素详细信息?

D3.js:根据最长字符串的宽度转换文本组

javascript - D3.js:将路径附加到矩形

传单在 map 中显示 .mbTiles 文件