전단지 및 geoJSON이있는 이벤트를 클릭하십시오. 실행하는 방법 나는 조사 onEachFeature했지만 geoJSON 이로

클릭 이벤트를 geoJSON에 첨부 한 다음 클릭 할 때 Ajax 기능을 실행하는 방법 나는 조사 onEachFeature했지만 geoJSON 이로 드 될 때 실행되며 클릭 할 때가 아니라 수많은 아약스 호출을 실행합니다!



답변

당신은 올바른 길을 가고있었습니다 onEachFeature.

각 요소에서 이벤트 클릭을 바인딩해야합니다.

아래 참조 (테스트)

function whenClicked(e) {
  // e = event
  console.log(e);
  // You can make your ajax call declaration here
  //$.ajax(... 
}

function onEachFeature(feature, layer) {
    //bind click
    layer.on({
        click: whenClicked
    });
}

geojson = L.geoJson(your_data, {
    style: style,
    onEachFeature: onEachFeature
}).addTo(map);

답변

ThomasG77 버전보다 약간 적은 코드로 수행 할 수 있습니다.

function onEachFeature(feature, layer) {
    //bind click
    layer.on('click', function (e) {
      // e = event
      console.log(e);
      // You can make your ajax call declaration here
      //$.ajax(... 
    });

}

geojson = L.geoJson(your_data, {
    style: style,
    onEachFeature: onEachFeature
}).addTo(map);

답변

인라인 함수와 다른 방법

geojson = L.geoJson(your_data, {
style: style,
onEachFeature: function onEachFeature(feature, layer) {

layer.on('mouseover', function (e) {
  // e = event
  console.log(e);
  // You can make your ajax call declaration here
  //$.ajax(... 
  });}).addTo(map);