2016-03-04 37 views
0

我試圖在互動地圖上重新歸一化的象徵符號成正比原始比例符號調用一個函數(傳單)重:象徵符號比例由單張

//Step 2: Import GeoJSON data 
function getData(map){ 
    //load the data 
    $.ajax("data/PropRaw2001_2013.geojson", { 
     dataType: "json", 
     success: function(response){ 

      //create an attributes array 
      var attributes = processData(response); 
      var rawAttributes = processRawData(response); 

function processData(data){ 
    //empty array to hold attributes 
    var attributes = []; 

    //properties of the first feature in the dataset 
    var properties = data.features[0].properties; 

    //push each attribute name into attributes array 
    for (var attribute in properties){ 
     //only take attributes with population values 
     if (attribute.indexOf("Prop") > -1){ 
      attributes.push(attribute); 
     }; 
    }; 

    //check result 
    console.log("these are the attributes") 
    console.log(attributes); 

    return attributes; 
}; 

function processRawData(data){ 
    console.log("Raw data") 
    //empty array to hold attributes 
    var rawAttributes = []; 
    console.log(rawAttributes) 
    //properties of the first feature in the dataset 
    var properties = data.features[0].properties; 

    //push each attribute name into attributes array 
    for (var rawAttribute in properties){ 
     //only take attributes with population values 
     if (rawAttribute.indexOf("Raw") > -1){ 
      rawAttributes.push(rawAttribute); 
      console.log(rawAttributes) 
     }; 
    }; 

這裏,是函數,將更新我的比例符號大小:

function updatePropSymbols(map, attribute){ 
    map.eachLayer(function(layer){ 
     //Example 3.16 line 4 
     if (layer.feature && layer.feature.properties[attribute]){ 
      //access feature properties 
      var props = layer.feature.properties; 

      //update each feature's radius based on new attribute values 
      var radius = calcPropRadius(props[attribute]); 
      layer.setRadius(radius); 

      //add city to popup content string 
      var popupContent = "<p><b>City:</b> " + props.City + "</p>"; 

      //add formatted attribute to panel content string 
      var year = attribute.split("_")[1]; 
      popupContent += "<p><b>Population in " + year + ":</b> " + props[attribute] + " million</p>"; 

      //replace the layer popup 
      layer.bindPopup(popupContent, { 
       offset: new L.Point(0,-radius) 
      }); 
     }; 
    }); 
}; 

我添加了兩個按鈕爲div的「歸」和「生」,我希望用戶能夠使用標準化的原始,使用以下功能之間切換:

$("#Normalized").click(function(attribute){ 
      console.log("normalize function") 
      //normalize = true 
      //if (normalize = true) { 

       updatePropSymbols(map, attribute); 
      //} 
     }); 
// 
$("#Raw").click(function(rawAttribute){ 
      //normalize = false 
      //if (normalize = false){ 
       updatePropSymbols(map, rawAttribute); 
      //} 
    }); 

但它沒有傳遞屬性數據。看起來我無法用rawAttribute或attribute(這是規範化的)調用updatePropSymbols。誰能幫我嗎?對不起,這麼長的代碼!

回答

0

通過使用attributerawAttribute爲你的點擊處理函數的參數,你實際上是在創造新的局部變量(在這種情況下,他們是jQuery的eventObjects說明click事件)將被用來代替原來的attributerawAttribute變量。假設你在別處定義這些變量,你要的是這樣的:

$("#Normalized").click(function(){ 
    updatePropSymbols(map, attribute); 
}); 

$("#Raw").click(function(){ 
    updatePropSymbols(map, rawAttribute); 
}); 

這裏是說明一個簡單的例子:http://jsfiddle.net/nathansnider/zhLLydcr/