2017-02-21 26 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/