2017-05-02 69 views
1

我在Mapbox中繪製LineStrings。現在,我正在更新屬性更改時的線條顏色。僅更新那些屬性

function update_the_map(){ 
    $.ajax({ 
    url:"http://dataurl", 
    contentType : "application/json; charset=utf-8", 
    type: "GET", 
    dataformat:"JSON", 
    async : false, 
    success: function(data){ 

     for (i = 0; i <lines.features.length; i++) { 
      lines['features'][i]['properties']['points']=data[i].points; 
      var style_update =getColor(lines['features'][i]['properties']['points']); 
      geojson.setFeatureStyle(lines['features'][i]['properties']['id'], style_update); 
     } 

     setTimeout(update_the_map, 10000); 
     console.log("updated"); 
    }, 
    error:function(){} 
    }); 
} 

但是,這改變了線路的所有顏色,而不是這些點都大於5.Because我取色,功能就像是

function getColor(d) { 
      if(d==10 || d==9 || d==8 || d==7 || d==6){ 
       return '#ff0000'; 
      } 
      else { 
       return '#00a1ff'; 
      } 
    } 

如果點> 5,否則返回所以它返回紅blue.But這一切都會返回藍色,並且整個線條的顏色都會發生變化。如果有幫助,我會很感激。這就是我創建圖層的方式。

geojson = L.vectorGrid.slicer(lines, { 
    vectorTileLayerStyles: { 
     sliced: style}, 

    maxZoom: 24, // max zoom to preserve detail on 
    interactive: true, 
    getFeatureId: function(f) { 
     return f.properties.id; 
    } 
}).on('mouseover', mouseover_function).addTo(map); 

我行是如下的變量:

var lines= { 

      "type":"FeatureCollection","features": [{"type": "Feature","geometry":{"type":"LineString", 
"coordinates":[[ 101.942139,4.252606],[101.766357,3.134346]]}, 
"properties": {"id":"01","points":10}},.... 
    ]}; 
+1

當你創建圖層時,'links'和'style'的值是什麼? – IvanSanchez

+0

當我創建圖層時,所有的點都是10。所以每一行都是紅色的。 10秒後他們改變了>> 5變紅,然後<5變藍。但是現在我調試了我在'var style_update = getColor(lines ['features'] [i] ['properties'] ['points'])得到的顏色;'這是根據點。所以我覺得我錯了某處設置線條的樣式。 – Ricky

回答

0

以下確實使更新像魅力一樣工作。所以只傳遞顏色,我通過重量和不透明度和顏色,它的工作很好。

function update_the_map(){ 
     $.ajax({ 
     url:"http://dataurl", 
     contentType : "application/json; charset=utf-8", 
     type: "GET", 
     dataformat:"JSON", 
     async : false, 
     success: function(data){ 
      var style_update; //changed declaration here 

     for (i = 0; i <lines.features.length; i++) { 
      lines['features'][i]['properties']['points']=data[i].points; 
      style_update = { 
              weight: 2, 
              opacity: 1,            
              color: getColor(links['features'][i]['properties']['score']),                   
              fillOpacity: 0.7 
          };//added this whole for variable style update 

geojson.setFeatureStyle(links['features'][i]['properties']['id'],style_update); 
                } 

     setTimeout(update_the_map, 10000); 
     console.log("updated"); 
    }, 
    error:function(){} 
    }); 
} 
1

你能證明什麼是line.features陣列內或檢查d的控制檯值,傳遞給的getColor功能。

+0

當我打印傳遞給getColor函數時,他們會得到什麼顏色?他們根據得分獲得顏色(例如'9是傳遞給紅色的點,3是傳遞給藍色的分數,9是傳遞給得到紅色')。因此,這澄清了錯誤只是繪製顏色。因爲每次我根據點獲得正確的顏色,但是當我嘗試根據某處的顏色設置樣式時,我正在犯這個錯誤。 – Ricky