使用 OpenLayers,如何在单个图层上显示不同功能的不同图标?

Using OpenLayers, how can I display different icons for different features on a single layer?

首先,我总体上是 Openlayers/JS 的新手,对一般的编程经验相当缺乏,因此我的代码可能存在我不知道的其他问题。

我正在使用最新版本的 Openlayers (5.3.0)。

我的程序目前通过 Ajax 传递 GeoJson 格式的数据以显示在 Openlayers 地图上。它为要显示的要素创建地图、视图和图层。当我按下页面上的 "Go" 按钮时,要素已成功加载到地图上。在我的例子中,特征只是简单的点 latitude/longitude 使用 png 标记来可视化。在被序列化并发送到我页面上的 JS 进行反序列化之前,GeoJson 在 C# 中看起来像这样:

{{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -1.549077,
          53.800755
        ]
      },
      "properties": {
        "GPSKey": 1,
        "Latitude": 53.800755,
        "Longitude": -1.549077,
        "TimeAdded": "2019-01-15T12:10:16",
        "IconPath": "pinred.png"
      },
      "ID": 1,
      "IconPath": null
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -1.545077,
          53.800755
        ]
      },
      "properties": {
        "GPSKey": 2,
        "Latitude": 53.800755,
        "Longitude": -1.545077,
        "TimeAdded": "2019-01-15T12:10:16",
        "IconPath": "pinred.png"
      },
      "ID": 2,
      "IconPath": null
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -1.524043,
          53.773222
        ]
      },
      "properties": {
        "GPSKey": 3,
        "Latitude": 53.773222,
        "Longitude": -1.524043,
        "TimeAdded": "2019-01-15T12:10:16",
        "IconPath": ""
      },
      "ID": 3,
      "IconPath": null
    }
  ]
}}

JS接收到上面的序列化后使用这段代码添加到图层中查看:

var geojsonFormat = new ol.format.GeoJSON({
        dataProjection: "EPSG:4326",
        featureProjection: "EPSG:3857"
    });//creates a format definition

    jsonDecoded = JSON.parse(result); /

    if (jsonDecoded.features.length > 0) {
        for (var i = 0; i < jsonDecoded.features.length; i++) {
            vectorSource.addFeature(geojsonFormat.readFeature(jsonDecoded.features[i], { featureProjection: "EPSG:3857" }));

        }

    }/

它添加到的矢量图层如下所示:

var vectorLayer = new ol.layer.Vector({
    source: vectorSource,
    style: iconStyleFunc()
});

而 iconStyleFunc() 看起来像这样:

function iconStyleFunc() {

    var zIndex = 1;

    var iconName = null;

    if (iconName == null) {
        iconName = "pinother.png"
    };


    iconStyle = [new ol.style.Style({
        image: new ol.style.Icon(({
            anchor: [0.5, 36], 
            anchorXUnits: "fraction",
            anchorYUnits: "pixels",
            opacity: 1,
            src: "images/" + iconName,  
            zIndex: zIndex
        })),
        zIndex: zIndex
    })];
return iconStyle;

这适用于使用图标 "pinother.png" 设置所有功能的样式。当我按下按钮时,我在地图上显示点没有问题。

我想做的是根据每个功能的 GeoJson "iconpath" 属性中的图标路径应用样式,这样任何具有 "pinred.png" 的功能都会使用它而不是默认 "pinother.png",等等,我将来可能需要添加各种图标。

我不确定如何阅读每个功能的 属性 以及如何在样式功能中最好地实现它。我设想的方式是使用 iconStyleFunc() 遍历功能,读取每个功能的 IconPath 属性,将该值附加到 iconStyleFunc() 中的 "src/images/" 路径,并适当地设置功能样式。

使用样式函数的特征参数,您可以获得特征的属性

function iconStyleFunc(feature) {

    var zIndex = 1;

    var iconName = feature.get("IconPath") || "pinother.png";

    iconStyle = [new ol.style.Style({
        image: new ol.style.Icon(({
            anchor: [0.5, 36], 
            anchorXUnits: "fraction",
            anchorYUnits: "pixels",
            opacity: 1,
            src: "images/" + iconName,  
            zIndex: zIndex
        })),
        zIndex: zIndex
    })];
return iconStyle;