2017-02-21 62 views
1

我有結構化的這樣一個GeoJSON的文件:是否可以從GeoJson中爲openlayers JavaScript地圖提取樣式信息?

{ 
"type": "FeatureCollection", 
"features": [{ 
    "type": "Feature", 
    "properties": { 
     "marker-color": "#4620dd", 
     "marker-size": "medium", 
     "marker-symbol": "park", 
     "name": "State Park" 
    }, 
    "geometry": { 
     "type": "Point", 
     "coordinates": [-76.95266723632812, 
      39.07974903895123 
     ] 
    } 
}] 

}

我能夠在地圖的OpenLayers創建從這個GeoJSON的一個矢量圖層,但無法利用的樣式屬性。我應該使用自定義樣式函數來執行此操作嗎?

+0

是的,你應該使用ol.style風格你層。 – FatAl

回答

0

是的,肯定的:

var vectorLayer = new ol.layer.Vector({ 
    source: vectorSource, 
    style: function (feature, resolution) { 
    console.log(feature.getProperties()); // <== all geojson properties 
    return [new ol.style.Style({ 
     image: new ol.style.Circle({ 
     radius: 10, 
     fill: new ol.style.Fill({ color: feature.get('marker-color') }) 
     }) 
    })]; 
    } 
}); 

https://jsfiddle.net/jonataswalker/uvopawmg/