使用OpenLayers,如何在一个图层上显示不同功能的不同图标? [英] Using OpenLayers, how can I display different icons for different features on a single layer?
问题描述
首先,我总体上是Openlayers/JS的新手,并且总体上对编程没有任何经验,因此我的代码可能还有其他我不知道的问题.
firstly I'm new to Openlayers/JS as a whole and fairly inexperienced with programming in general so there might be other problems with my code that I'm not aware of.
我正在使用最新版本的Openlayers(5.3.0).
I am using the latest version of Openlayers (5.3.0).
我的程序当前通过Ajax传递GeoJson格式的数据,以显示在Openlayers地图上.它为要显示的要素创建地图,视图和图层.当我按页面上的开始"按钮时,要素将成功加载到地图上.在我的情况下,特征只是使用png标记进行可视化的具有纬度/经度的简单点.在被序列化并发送到我页面上的JS进行反序列化之前,GeoJson在C#中看起来像这样,
My program currently passes GeoJson formatted data via Ajax to be displayed on an Openlayers map. It creates the map, view and a layer for the features to be displayed on. When I press a "Go" button on the page, the features are loaded onto the map successfully. In my case the features are just simple points with latitude/longitude using a png marker to visualise. The GeoJson looks like this in C# before being serialised and sent to JS on my page for deserialisation:
{{
"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接收到以上序列化的代码,并使用此代码将其添加到图层中以供查看:
The JS receives the above serialised and uses this code to add it to the layer for viewing:
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" }));
}
}/
它添加到的矢量层如下所示:
The vector layer it gets added to looks like this:
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: iconStyleFunc()
});
iconStyleFunc()如下所示:
And the iconStyleFunc() looks like this:
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"设置所有功能的样式效果很好.按下按钮时,我在地图上显示点没有问题.
This works fine for styling all the features with the icon "pinother.png". I have no problem displaying the points on the map when I press the button.
我想做的是基于每个要素的GeoJson"iconpath"属性中的图标路径应用样式,以便具有"pinred.png"的任何要素都将使用该样式,而不是默认的"pinother". png",等等,将来可能需要添加各种图标.
What I'd like to do is apply styling based on the icon path in the properties of each feature's GeoJson "iconpath", so that any feature having a "pinred.png" would use that instead of the default "pinother.png", and so on with various icons I might need to add in the future.
我不确定如何读取每个功能的此属性以及如何最好地在样式函数中实现它.我设想的方式是使用iconStyleFunc()遍历功能,读取每个功能的IconPath属性,将该值附加到iconStyleFunc()的"src/images/"路径中,然后对功能进行适当的样式设置.
I'm not sure how to read this property of each feature and how I would best implement it in the styling function. The way I envisaged it was iterating through features using the iconStyleFunc(), reading the IconPath property for each feature, appending that value to the "src/images/" path in the iconStyleFunc() and styling the feature appropriately.
推荐答案
使用样式函数的feature参数,您可以获取功能的属性
Using the feature argument of the style function you can get properties of the feature
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,如何在一个图层上显示不同功能的不同图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!