为每组坐标绘制具有不同样式的开放图层图层

Draw an openLayers layer with different style for each set of coordinates

本文关键字:图层 样式 坐标 绘制      更新时间:2023-09-26

我有一个从服务器得到的刺痛,我需要循环它。这个字符串实际上是我想使用 openLayers 在地图上可视化的位置的地理。下面是返回字符串的示例:

{crs:
    {"type":"name",
     "properties":{
            "name":"ESPG:4326"
      },
     },
     "features":[{"properties":{
                 "image":"IMAGE1"},
                 "type":"Feature",
                 "geometry":{
                           "type":"Point",
                           "coordinates":[0,0]
                  }},{"properties":{
                 "image":"IMAGE2"},
                 "type":"Feature",
                 "geometry":{
                           "type":"Point",
                           "coordinates":[10,10]
                  }},{"properties":{
                 "image":"IMAGE3"},
                 "type":"Feature",
                 "geometry":{
                           "type":"Point",
                           "coordinates":[75,99]
                  }}]
        ,type:"FeatureCollection"
}

如您所见,features的每个feature都有一些properties和一个geometry。我想创建一个新layer(ol.layer.Vector),对于特征的每个元素,它将绘制一个具有相应坐标和图像的点。使用以下代码,我设法使用预定义的图像绘制地图中的所有点。我想我应该遍历这个字符串并获取坐标和图像,并以某种方式创建一个图层(或许多图层?任何想法??谢谢!

//map is the actual map where the images load
//responseText is the String that I get from the server
//predefinedImage is actually a predefined image that I use as icon for the points.
//If I complitelly remove the "style" from geojson_layer it sets the image to a default value.
var geoJSONsource = new ol.source.Vector({
     features: (new ol.format.GeoJSON()).readFeatures(responseText)
});
geojson_layer = new ol.layer.Vector({
     source: geoJSONsource,
     style: predefinedImage
});
map.addLayer(geojson_layer);

这可能有助于您:

https://gis.stackexchange.com/a/95389/77349

但是,您应该修改此行:

externalGraphic: 'marker.png',  

到类似的东西

externalGraphic: this.image,  

希望这有帮助!