2014-01-28 64 views
3

我正在創建特定狀態的地圖,我一直在試驗d3.js和topojson並創建了一個很棒的地圖,但現在我想添加一個標記在地圖上。將標記放置到使用topoJSON和d3.js生成的地圖中

但現在我有問題,因爲我添加標記有一個GeoJSON文件添加到生成的地圖的標記,也是我想打開工具提示每當一個標記被按下的權力。

我的地圖與此非常相似:http://bl.ocks.org/mbostock/4699541我想要的是通過具有標記地理座標的GeoJSON文件將標記添加到狀態。

So the map is currently 所以地圖是目前

Map expected... 地圖預計...

+1

你見過[地圖教程](http://bost.ocks.org/mike/map/)?它描述瞭如何將標記添加到地圖。 –

回答

10

你可以在你的JSON回調結束這樣的財產以後添加:

var marks = [{long: -75, lat: 43},{long: -78, lat: 41},{long: -70, lat: 53}]; 

svg.selectAll(".mark") 
    .data(marks) 
    .enter() 
    .append("image") 
    .attr('class','mark') 
    .attr('width', 20) 
    .attr('height', 20) 
    .attr("xlink:href",'https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/24x24/DrawingPin1_Blue.png') 
    .attr("transform", function(d) {return "translate(" + projection([d.long,d.lat]) + ")";});