【问题标题】:Using OpenLayers, how can I display different icons for different features on a single layer?使用 OpenLayers,如何在单个图层上显示不同功能的不同图标?
【发布时间】:2019-01-23 11:44:44
【问题描述】:

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

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

我的程序目前通过 Ajax 传递 GeoJson 格式的数据以显示在 Openlayers 地图上。它为要显示的要素创建地图、视图和图层。当我按下页面上的“开始”按钮时,这些功能已成功加载到地图上。在我的情况下,这些特征只是使用 png 标记进行可视化的具有纬度/经度的简单点。 GeoJson 在 C# 中看起来像这样,然后被序列化并发送到我页面上的 JS 进行反序列化:

{{
  "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”。 png",等等,还有我将来可能需要添加的各种图标。

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

【问题讨论】:

    标签: javascript ajax gis geojson openlayers-5


    【解决方案1】:

    使用样式函数的特征参数可以获取特征的属性

    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;
    

    【讨论】:

    • 谢谢迈克!我想我把事情复杂化了,并试图编写巨大的函数来迭代每个特性并在 Openlayers 已经内置该样式时应用样式。我想我必须将该特性显式传递给 Vectorlayer“样式”属性中的 iconStyleFunc但只需调用 iconStyleFunc 就足以让它工作。
    • 我不明白你在传递特征变量。你能提供更多信息吗?因为我在这里看到的,会让每个图标看起来都一样,不是吗?
    • 它使用功能的IconPath 属性来生成特定于每个功能的图标,
    • 我和@RaphaëlBalet 在一起 - 请问feature 是如何传递给iconStyleFunc 的?否则它看起来很棒。
    • OpenLayers 在渲染图层时为每个要素调用样式函数。样式应仅使用函数名称 style: iconStyleFunc 定义 - 不应通过在图层设置中添加 () 来调用该函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多