【问题标题】:How do I calculate a bounding box for leaflet from a set of points in d3如何从 d3 中的一组点计算传单的边界框
【发布时间】:2015-02-11 20:26:47
【问题描述】:

我正在从 d3/crossfilter 数据集(从 CSV 加载)向传单地图添加点。添加点后,我想缩放/平移以使点在视图中。最简单的方法是什么...我想我可以编写一些最小/最大经度/纬度函数,然后遍历数据集以创建一个与 map.fitBound 一起使用的边界框,但想知道是否有更简单的方法...

代码 sn-p:-

function updateMapData() {
    g.selectAll("*").remove();
    var feature = g.selectAll("circle")
        .data(byID.top(Infinity))
        .enter()
        .append("circle")
            .style("stroke", "black")  
            .style("opacity", .95) 
            .style("fill", "black")
            .attr("r", 4);

    map.on("viewreset", updateMap);
    updateMap(); 

    function updateMap() {
        feature.attr("transform", 
        function(d) { 
            return "translate("+ 
                map.latLngToLayerPoint(d.LatLng).x +","+ 
                map.latLngToLayerPoint(d.LatLng).y +")";
            }
        )
    };
};

【问题讨论】:

  • 您可以从这些点构造一个多边形,然后将地图拟合到那个位置。
  • 最后,我只运行以下命令(但我仍然想知道是否应该使用 geoJSON 或 d3.geo 来代替......):- 'bounds = []; byID.top(Infinity).forEach(function (d) { bounds.push(d.LatLng); }); map.fitBounds(bounds);'

标签: d3.js leaflet


【解决方案1】:

最后,我只运行了以下命令(但我仍然想知道是否有更简单的方法,或者我是否应该使用 geoJSON 或 d3.geo 来代替......)

bounds = [];
byID.top(Infinity).forEach(function (d) {
    bounds.push(d.LatLng);
});  
map.fitBounds(bounds, {pan: {animate: true, duration: 1.5, easeLinearity: 0.25}});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    相关资源
    最近更新 更多