2017-01-24 3 views
2

私はリーフレットで構築されたマップ上で作業しており、ヨーロッパや世界の他の地域を構成する国境のフィーチャーであるtopoJSONファイルを持っています。フィーチャコレクションには、各国の属性名が含まれています。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

それは準備ができた後、topojsonですべての層を反復処理するためにLeaflet's eachLayer functionを使用し、リーフレット雑食動物を使用して、各レイヤにツールチップをバインドします。

など。

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/

関連する問題