2017-03-09 61 views
1

我試圖更改單張1.0.3中的一行線條樣式。我的圖層是使用ajax調用和leaflet-ajax庫生成的geoJSON圖層。我可以在地圖上看到我的圖層,但它繼承了默認樣式,而不是我想要添加的樣式。如何更改Leaflet 1.0.3中的LineStrings的geoJSON圖層的樣式

var lines = new L.GeoJSON.AJAX('/rest/lines'); 

lines.setStyle({color:"#00000",weight:10}).addTo(map); 

var overlays = {"Lines": lines}; 

L.control.layers(overlays).addTo(map); 

回答

2

您應該嘗試定義一個函數,該函數會將每行加載到Leaflet中時設置樣式。

從這個鏈接:https://github.com/Dominique92/Leaflet.GeoJSON.Ajax

... 
new L.GeoJSON.Ajax(
    <URL>, // GeoJson server URL. 
    { 
     argsGeoJSON: { 
      name: value, // GeoJson args pairs that will be added to the url with the syntax: ?name=value&... 
      ... 
     } 
     bbox: <boolean>, // Optional: whether or not add bbox arg to the geoJson server URL 
     style: function(feature) { // Optional 
      return { 
       "<NAME>": <VALUE>, // Properties pairs that will overwrite the geoJson flow features properties 
       "<NAME>": feature.properties.<NAME>, // The value can be calculated from any geoJson property for each features. 
       ... 
      }; 
     } 
    } 
).addTo(map); 
... 

這是我的代碼,這是形狀不是線,而是應該以類似的方式工作:

geojson = L.geoJson(myGeoJson.features, { 
    onEachFeature: onEachFeature, 
    style: styleFeature, 
}).addTo(myLeafletMap); 

,然後我具備的功能:

function onEachFeature(feature, layer) { 
... 
} 

function styleFeature(feature){ 
    return { 
     weight: 2.5, 
     opacity: 1, 
     color: getColour('blue'), 
    }; 
} 
+0

我錯過了在文檔中。謝謝你指出。 – MakleBirt

相關問題