是否可以从GeoJson中提取样式信息以获取openlayers javascript地图?

时间:2017-02-21 19:43:34

标签: javascript openlayers geojson

我有一个像这样结构的geojson文件:

{
"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创建矢量图层,但无法使用样式属性。我应该使用自定义样式函数来执行此操作吗?

1 个答案:

答案 0 :(得分: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/