2013-07-10 21 views
1

我從GeoJSON文件載入地圖數據併爲每個多邊形附加一個單擊事件。點擊後,腳本應該從服務器獲取數據並修改其中一個點擊的多邊形屬性。即:傳單載入數據並編輯鼠標點擊屬性

function onClick(e) { 
    var status = e.target.feature.properties.ACCESS; 
    $.ajax({ 
     url: "http://127.0.0.1:8080/?&u=x&p="+e.target.feature.properties.ID_PARCELL, 
     dataType: 'jsonp', 
     type: 'GET', 
     success: function(data) { 
     status = data.status; 
     e.target.feature.properties.ACCESS = data.status; 
     e.target.bindPopup("Owner: <b>"+ data.status +"</b>").openPopup(); 
     }, 
     error: function(data){console.log(data)} 
    }); 
    e.target.feature.properties.ACCESS = status; 
    map.fitBounds(e.target.getBounds()); 
} 

但由於成功函數是一個回調(同步與否,其實並不重要),我無法找回原來的事件源(即e),所以我可以修改其屬性之一。

我的問題是:如何在加載此數據後返回事件源?有沒有任何通用的Javascript方式?如果沒有,有沒有什麼辦法可以通過功能ID查詢GeoJSON圖層? (=>因此,我可以在ajax調用中發送功能ID,並簡單地將其返回迴應)

回答

0

您在使用Click和成功功能時使用相同的事件名稱'e'。嘗試改變其中之一。即

function onClick(e) { 
    var status = e.target.feature.properties.ACCESS; 
    $.ajax({ 
     url: "http://127.0.0.1:8080/?&u=x&p="+e.target.feature.properties.ID_PARCELL, 
     dataType: 'jsonp', 
     type: 'GET', 
     success: function(data, evt) { 
      status = data.status; 
      e.target.feature.properties.ACCESS = data.status; 
      e.target.bindPopup("Owner: <b>"+ data.status +"</b>").openPopup(); 
     }, 
     error: function(data){console.log(data)} 
    }); 
    e.target.feature.properties.ACCESS = status; 
    map.fitBounds(e.target.getBounds()); 
} 

現在返回結果時,你可以更改初始事件「E」性能。
希望這有助於。

+0

沒有啊......我應當予以更正。該代碼是錯誤的...我的壞! ! – FearUs

+0

成功的論點沒有通過 – FearUs

+0

我的意思是這是一些實驗性的東西,我沒有真正清理之前張貼在這裏。現在它很乾淨。所以沒有成功的通過。你有任何線索嗎? – FearUs

4

的解決方案是使用上下文項目來發送所需的變量e匿名函數:

function onClick(e) { 
    var status = e.target.feature.properties.ACCESS; 
    $.ajax({ 
     url: "http://127.0.0.1:8080/?&u=x&p="+e.target.feature.properties.ID_PARCELL, 
     dataType: 'jsonp', 
     context: e, 
     type: 'GET', 
     success: function(data) { 
     status = data.status; 
     e.target.feature.properties.ACCESS = data.status; 
     e.target.bindPopup("Owner: <b>"+ data.status +"</b>").openPopup(); 
     }, 
     error: function(data){console.log(data)} 
    }); 
    e.target.feature.properties.ACCESS = status; 
    map.fitBounds(e.target.getBounds()); 
} 
+0

一旦找到解決方案,發佈解決方案的榮譽。很少有人這樣做 - 謝謝。 – Leeward