2014-12-13 194 views
2

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

代码片段: -

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 +")"; 
      } 
     ) 
    }; 
}; 
+0

你可以从点构造一个多边形,然后适合地图。 – 2014-12-13 12:05:29

+0

最后,我只是运行以下(但我仍然想知道是否应该使用geoJSON或d3.geo,而不是...): - 'bounds = []; byID.top(Infinity).forEach(function(d){ bounds.push(d.LatLng); }); map.fitBounds(bounds);' – Murray 2014-12-14 04:11:52

回答

0

最后,我只是跑下面的(但我仍然不知道是否有更简单的方法,或者我应该使用以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}});