简体   繁体   English

使用外部 geoJSON 文件中的数据更改 Leaflet 标记图标

[英]Change Leaflet marker icon with data from external geoJSON file

I have a problem at changing the icons of my geoJSON data on my Leaflet map.我在更改 Leaflet map 上的 geoJSON 数据图标时遇到问题。

I'm loading my data from external geoJSON files.我正在从外部 geoJSON 文件加载我的数据。 Therefore I'm using a XMLHttpRequest() to open the files (filenames are stored in an array orteDB ) and then add them to my layer orte where it's then added to the map:因此,我使用XMLHttpRequest()打开文件(文件名存储在数组orteDB ),然后将它们添加到我的层orte ,然后将其添加到 map:

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);

I already managed to change the default icon with the help of this example https://gist.github.com/geog4046instructor/80ee78db60862ede74eacba220809b64 .我已经设法借助此示例https://gist.github.com/geog4046instructor/80ee78db60862ede74eacba220809b64更改默认图标。 But what I actually want is the icon to change dynamically after the click of a button or the marker itself to this redIcon :但我真正想要的是在单击按钮或标记本身到此redIcon后动态更改的图标:

var redIcon = L.Icon.extend({
        iconUrl: 'files/marker-icon_red.png',
        iconSize:     [25, 41],
        iconAnchor:   [12, 40],
    });

I also tried working with the pointToLayer() function but it didn't work for me.我也尝试使用pointToLayer() function 但它对我不起作用。

Each geoJSON point has a unique ID which I can call via e.layer.feature.properties.id .每个 geoJSON 点都有一个唯一的 ID,我可以通过e.layer.feature.properties.id调用它。 So in the end I'd like to change the marker icon individually by using it's unique ID.所以最后我想通过使用它的唯一 ID 单独更改标记图标。

My geoGSON files look like this:我的 geoGSON 文件如下所示:

{
 "type": "FeatureCollection",
 "name": "02_Roncesvalles",
 "features": [
  { 
   "type": "Feature", 
   "properties": 
     { 
      "id": 2 }, 
   "geometry": 
     { 
      "type":"Point", 
      "coordinates": [ 
        -1.320700314538876, 
        43.009378247303687 ] 
     } 
   }
  ]
}

Thank you very much for your help!非常感谢您的帮助!

You can change the icon of a marker with marker.setIcon(icon) and you need to loop through all layers and find the correct one over the id.您可以使用marker.setIcon(icon)更改标记的图标,您需要遍历所有层并在 id 上找到正确的层。

I use map.eachLayer instead of orte.eachLayer() because the GeoJSON Group can also contains LayerGroups.我使用map.eachLayer而不是orte.eachLayer()因为 GeoJSON Group 也可以包含 LayerGroups。

var wantedId = 1;
map.eachLayer((layer)=>{
   if(layer instanceof L.Marker && layer.feature && layer.feature.properties && layer.feature.properties.id == wantedId){
      layer.setIcon(new redIcon());
   }
});

Update更新

Thx to @ghybs, I didn't recognice the extend of the icon.谢谢@ghybs,我没认出图标的extend

If you want to extend the icon class you need to put the options in a options property.如果你想扩展图标 class 你需要将选项放在options属性中。

var redIcon = L.Icon.extend({
   options: {
        iconUrl: 'files/marker-icon_red.png',
        iconSize:     [25, 41],
        iconAnchor:   [12, 40],
   }
});

And then you can change the Icon with marker.setIcon(new redIcon())然后您可以使用marker.setIcon(new redIcon())更改图标

But I suggest you to simply create a Icon variable instead of creating a new class.但我建议您简单地创建一个图标变量,而不是创建一个新的 class。

var redIcon = L.icon({
        iconUrl: 'files/marker-icon_red.png',
        iconSize:     [25, 41],
        iconAnchor:   [12, 40],
    });

And then call marker.setIcon(redIcon)然后调用marker.setIcon(redIcon)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM