【发布时间】:2021-02-17 20:11:30
【问题描述】:
我在更改传单地图上的 geoJSON 数据图标时遇到问题。
我正在从外部 geoJSON 文件加载我的数据。因此,我使用XMLHttpRequest() 打开文件(文件名存储在数组orteDB 中),然后将它们添加到我的图层orte,然后将其添加到地图中:
for (var i = 0; i < orteDB.length; i++) {
let xhr = new XMLHttpRequest();
xhr.open('GET', 'files/geoJSONfiles/orte/' + orteDB[i]);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.responseType = 'json';
xhr.onload = function () {
if (xhr.status !== 200) return
L.geoJSON(xhr.response).addTo(orte).on('click', onClick);
};
xhr.send();
}
orte = L.geoJson(orte).addTo(map);
借助此示例https://gist.github.com/geog4046instructor/80ee78db60862ede74eacba220809b64,我已经设法更改了默认图标。但我真正想要的是在单击按钮或标记本身到此redIcon 后动态更改的图标:
var redIcon = L.Icon.extend({
iconUrl: 'files/marker-icon_red.png',
iconSize: [25, 41],
iconAnchor: [12, 40],
});
我也尝试过使用 pointToLayer() 函数,但它对我不起作用。
每个 geoJSON 点都有一个唯一的 ID,我可以通过 e.layer.feature.properties.id 调用它。所以最后我想通过使用它的唯一 ID 来单独更改标记图标。
我的 geoGSON 文件如下所示:
{
"type": "FeatureCollection",
"name": "02_Roncesvalles",
"features": [
{
"type": "Feature",
"properties":
{
"id": 2 },
"geometry":
{
"type":"Point",
"coordinates": [
-1.320700314538876,
43.009378247303687 ]
}
}
]
}
非常感谢您的帮助!
【问题讨论】:
标签: javascript leaflet icons geojson marker