d3.js - 缩放时如何让 d3 svg 点保持在传单 map 上的正确位置?

标签 d3.js leaflet

var map = L.map('mapid').setView([10.0, 15.0], 2);

L.tileLayer('https://api.mapbox.com/styles/v1/mapbox/light-v9/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1Ijoic2FidW1hZm9vIiwiYSI6ImNqMWE3cnlqcTA5dncyd216YjI0bnY4dGEifQ.fgmXgmkvialdBd3D405_BA', {
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18,
id: 'mapbox.streets',
accessToken: 'pk.eyJ1Ijoic2FidW1hZm9vIiwiYSI6ImNqMWE3cnlqcTA5dncyd216YjI0bnY4dGEifQ.fgmXgmkvialdBd3D405_BA'
}).addTo(map);

L.svg().addTo(map);

var svg = d3.select(map.getPanes().overlayPane).select('svg');

var g = svg.append('g').attr('class', 'leaflet-zoom-hide');

const URL = 'https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&limit=200'

function getEarthquakeData() {
  fetch(URL)
  .then((results) => {
return results.json();
  })
  .then((result) => {

function projectPoint(x,y) {
  var point = map.latLngToLayerPoint(new L.LatLng(y,x));
  this.stream.point(point.x, point.y);
}

var transform = d3.geoTransform({point: projectPoint}),
path = d3.geoPath().projection(transform);

function applyLatLngToLayer(d){
  var y = d.geometry.coordinates[1];
  var x = d.geometry.coordinates[0];
  return map.latLngToLayerPoint(new L.LatLng(y,x));
}

// console.log(result.features);
// result.features.forEach(function(d) {
//   d.LatLng = new L.LatLng(d.geometry.coordinates[1],
//     d.geometry.coordinates[0])
//   });

var circle = g.selectAll('circle')
.data(result.features)
.enter().append('circle')
.style('fill','darkred')
.attr('r', 2)
.attr('opacity',0.5);

function update() {

  circle.attr("transform",
  function(d) {
    return "translate("+
    applyLatLngToLayer(d).x+","+
    applyLatLngToLayer(d).y+")";
  });

  var bounds = path.bounds(result),
  topLeft = bounds[0],
  bottomRight = bounds[1];

  svg.attr("width", bottomRight[0] - topLeft[0])
    .attr("height", bottomRight[1] - topLeft[1])
    .style("left", topLeft[0] + "px")
    .style("top", topLeft[1] + "px");

  g.attr("transform", "translate(" + -topLeft[0] + "," + -topLeft[1] + ")");

  }

  map.on('viewreset', update);
  update();

  })

}


getEarthquakeData();

一旦获取了我感兴趣的 map 数据,上面的代码就会向传单 map 添加点。问题是,当我放大 map 时,所有点都保持在相同的位置,而 map 背景会改变 View 。除了打开的 View 之外,这些点实际上并未“固定”到任何 View 中的正确位置。我知道这与我更新 View 的方式有关,但我一直无法弄清楚。请帮忙!提前致谢。

最佳答案

我怀疑您使用的是版本 1.*,如果是这样,那么这是一个已知问题:

https://github.com/Leaflet/Leaflet/issues/5016

viewreset 事件切换到 moveend 事件,你应该没问题。

关于d3.js - 缩放时如何让 d3 svg 点保持在传单 map 上的正确位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44961539/

相关文章:

leaflet - 为什么 Leaflet/Mapbox 缩放和图层控件缺少标记?

leaflet - 如何在弹出窗口中隐藏尾部三角形

javascript - d3 : svg image in zoom circle packing

javascript - Leaflet 未读取外部 geojson 或 JS

D3.js donut chart 标签获得多三角形而不是多段线

javascript - 从 networkD3 的 forceNetwork 调整绘图的背景图片和标题

javascript - 将Leaflet标记添加到Leaflet中的geojson数据中

jquery - Leaflet不同的悬停和点击事件

javascript - 尝试删除dimple.js中的刻度标签

javascript - 下载 pdf/word 按钮 javascript/jquery