2017-01-24 67 views
2

我正在使用Leaflet構建的地圖上,並且有一個topoJSON文件,它是組成歐洲和世界其他地區的國家邊界的featureCollection。要素集合包含每個國家的屬性名稱。如何讓文本標籤顯示在topojson文件的多邊形中?

問:爲每個國家的多邊形建立一個國家名稱的建議方法是什麼?我應該使用傳單的工具提示功能嗎?我使用this example作爲起點,但無法顯示我的文本標籤。有任何想法嗎?我有一個topojson文件,其中包含我試圖從中提取的屬性。

My fiddle

var earth = L.geoJson(); 
earth.bindTooltip('text label', { 
    permanent: true, 
    direction: 'center' 
}); 


var naturalEarth = omnivore.topojson('https://gist.githubusercontent.com/dosstx/dcd1b4ebe3892527b12759226a21b900/raw/81abff4d455dbabe800c56aa7736dd4cbbfd1f64/topo.json', null, earth); 

回答

1

要工具提示結合使用單張雜食動物每一層,使用Leaflet's eachLayer function遍歷所有層在topojson,它已經準備好之後。

E.g.

var naturalEarth = omnivore.topojson('https://gist.githubusercontent.com/dosstx/dcd1b4ebe3892527b12759226a21b900/raw/81abff4d455dbabe800c56aa7736dd4cbbfd1f64/topo.json', null, earth) 
    .on('ready', function() { 
     naturalEarth.eachLayer(function(layer) { 
      //console.log(layer); //to inspect what properties are available 
      layer.bindTooltip(layer.feature.properties.FORMAL_EN, { 
       permanent: true 
      }); 
     }); 
    }); 

更新的jsfiddle:https://jsfiddle.net/kjLjhbe1/8/

相關問題