2012-04-21 125 views
10

我已经创建了一个从mbtile转换为geojson的地图,投影为WGS84。我加载它像:d3.js在d3.geo.path中添加一个圆圈

var map = svg.append("g").attr("class", "map"); 
var path = d3.geo.path().projection(d3.geo.albers().origin([3.4,46.8]).scale(12000).translate([590, 570])); 
    d3.json('myjsonfile.json', function(json) { 
     map.selectAll('path').data(json.features).enter().append('path').attr('d', path) 
}); 

现在我想用它(纬度,经度)在我的SVG坐标添加SVG元素(点,圆,点(我不知道)) 。

我不知道该怎么做。

回答

17

您需要分离出投影,所以你可以再次使用它来投影LAT贵点/ LON:

var map = svg.append("g").attr("class", "map"); 
var projection = d3.geo.albers() 
    .origin([3.4,46.8]) 
    .scale(12000) 
    .translate([590, 570]); 
var path = d3.geo.path().projection(projection); 
d3.json('myjsonfile.json', function(json) { 
    map.selectAll('path') 
     .data(json.features) 
     .enter().append('path').attr('d', path); 
    // now use the projection to project your coords 
    var coordinates = projection([mylon, mylat]); 
    map.append('svg:circle') 
     .attr('cx', coordinates[0]) 
     .attr('cy', coordinates[1]) 
     .attr('r', 5); 
}); 

另一种方式做,这是刚刚通过投影COORDS给点翻译:

map.append('svg:circle') 
    .attr("transform", function(d) { 
     return "translate(" + projection(d.coordinates) + ")"; 
    }) 
    .attr('r', 5);