Openlayers 3:如何更改 Geojson Icon src 以便它不会覆盖其他 geojson 类型?

Openlayers 3 : How to change Geojson Icon src so it won't override other geojson types?

我可以在加载 Point/MultiPoint Geojson 时更改图标的 src,方法如下:

 that.geojsonLayers[index]  = new that.openlayers.ol.layer.Vector({
      source: new that.openlayers.ol.source.Vector({
        format: new that.openlayers.ol.format.GeoJSON(),
        url: url         
      }),
      style: new that.openlayers.ol.style.Style({
        image: new that.openlayers.ol.style.Icon({
          src: 'http://mapmip.webiks.com/assets/Markers/marker-icon-blue.png'
        })
      })

但是我无法加载其他类型的 Geojson - 根本没有加载多边形,而几何集合(由图标和线条组成)仅加载图标。

更改图标 src 使其不会覆盖其他 geojson 类型的方法是什么?

您可以使用样式函数来验证需要设置样式的几何类型。设置图标来设置多边形的样式是不正确的。 检查这个

1.Declare你的风格

var myMultiStyle = {
//here replace with your icon style
        'Point': new ol.style.Style({
          image: new ol.style.Circle({
            fill: new ol.style.Fill({
              color: 'rgba(255,255,0,0.4)'
            }),
            radius: 5,
            stroke: new ol.style.Stroke({
              color: '#ff0',
              width: 1
            })
          })
        }),
        'LineString': new ol.style.Style({
          stroke: new ol.style.Stroke({
            color: '#f00',
            width: 3
          })
        }),
        'Polygon': new ol.style.Style({
         fill: new ol.style.Fill({
         color: 'rgba(0,255,255,0.5)'
         }),
         stroke: new ol.style.Stroke({
         color: '#0ff',
         width: 1
      })
    })
      };
  1. 创建样式函数

function myStyleFunction(feature,resolution){ return myMultiStyle[feature.getGeometry().getType()]; }

  1. 将样式函数分配给矢量源

that.geojsonLayers[索引]=新that.openlayers.ol.layer.Vector({ 来源:新 that.openlayers.ol.source.Vector({ 格式:新的 that.openlayers.ol.format.GeoJSON(), url: url<br> }), 样式:myStyleFunction })

检查 this official example 以查看结果。