2016-12-02 7 views
0

私は自分のカスタム 'latitude'と '経度'変数を持っています。リーフレット。緯度と経度でJSONマーカープロパティを取得するにはどうすればよいですか?

var custom_loc = [50.34434, 63.23442] 

そしてまた、私はGeoJSON形式でポイントを持つJSONデータを持っています。

{ 
    "type": "Feature", 
    "geometry": { 
    "type": "Point", 
    "coordinates": [50.34434, 63.23442] 
    }, 
    "properties": { 
    "id" : "1", 
    "name": "Good place" 
    } 
} 

どのように"custom_loc"JSONマーカーを見つけて(e.x. "ID"用)JSONプロパティを取得することができますか?

私は自分のプロジェクトでleaflet.jsを使用します。

答えて

0

マーカーgetLatLng()の方法を使用して、そのlatlngにアクセスし、それをあなたのカスタム位置と一致させることができます。 idをレイヤーにバインドするには、geoJSONフィーチャーをL.GeoJSONレイヤーに追加し、layer.feature.properties.idでIDにアクセスするか、またはonEachFeatureメソッドを経由してレイヤーにIDをバインドして、L.GeoJSONオプションに渡します。

次にあなたが例えばeachlayerメソッドを使用してにGeoJSON層を通してだけループを緯度と経度のマッチングを持つ層を発見したいとき:

var custom_loc = [50.34434, 63.23442]; 
var geoJSON = L.geoJson(
     { 
     "type": "FeatureCollection", 
     "features": [ 
     { 
      "type": "Feature", 
      "geometry": { 
      "type": "Point", 
      "coordinates": [50.34434, 63.23442] 
      }, 
      "properties": { 
      "id" : "1", 
      "name": "Good place" 
      } 
     } 
     ] 
    }, 
    { 
     onEachFeature: function(feature, layer) { 
     layer.options.id = feature.properties.id; 
     } 
    } 
); 
map.addLayer(geoJSON); 

geoJSON.eachLayer(l => { 
    var coords = l.feature.geometry.coordinates; 
    var latlng = l.getLatLng(); 
    if (latlng.lat === custom_loc[1] && latlng.lng === custom_loc[0]) { 
    console.log(`Latlng match: ${l.options.id}`) 
    } 
    if (coords[0] === custom_loc[0] && coords[1] === custom_loc[1]) { 
    console.log(`Latlng match: ${l.options.id}`); 
    } 
}); 
関連する問題