javascript - MapBox:为点分配图标

标签 javascript mapbox mapbox-gl-js

我正在尝试将图标分配给 GeoJSON 中的点。我使用这个片段:

    map.addSource('Punti di allerta', {
        type: 'geojson',
        data: source_attentionpoints, 
    });
    map.addLayer({
        'id': 'Punti di allerta',
        'type': 'circle',
        'source': 'Punti di allerta',
        'layout': {
            'icon-image': '{% static 'image/webgis/icons/warning50.png' %}',
            'icon-size': 0.5
        }
    });

我看到这两个错误,并且点未呈现:

Error: "layers.Punti di allerta.layout.icon-image: unknown property "icon-image"" Error: "layers.Punti di allerta.layout.icon-size: unknown property "icon-size""

但是如果我使用这个片段而不是之前的片段,我可以毫无问题地看到所有点:

map.addLayer({
    'id': 'Punti di allerta',
    'type': 'circle',
    'source': 'Punti di allerta',
     'paint': {
         'circle-radius': 8,
         'circle-color': 'rgb(0,0,0)',
         'circle-opacity': 1.0,
         'circle-stroke-width': 4,
         'circle-stroke-color': 'rgb(200,200,200)',
         'circle-stroke-opacity': 1.0,
         'circle-blur': 0,
    }
});

我做错了什么?

最佳答案

我发现问题了。我的代码中有两个错误:

  1. 'type': 'circle' 而不是 'type': 'symbol'
  2. 我在添加图层之前需要这个:

    map.loadImage(
        '{% static 'image/webgis/icons/warning50.png' %}',
        function(error, image) {
            if (error) throw error;
            map.addImage('warning', image);
            }
    );
    

关于javascript - MapBox:为点分配图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59703611/

相关文章:

javascript - 如何将带有转义字符的字符串转换为 "normal"字符串

javascript - 确认窗口的问题

javascript - 重置 Mapbox 中的所有标记层

mapbox - 如何在国家/地区掩盖/修剪 voronoi 多边形?

javascript - 设置复杂嵌套对象的格式

javascript - mapbox studio 中可见的图标未通过 mapbox gl 显示

javascript - react map -GL : Map Does Not Appear

javascript - 对象不支持属性或方法 'autocomplete'

javascript - 将 Javascript 数组传递给 Flask

javascript - 使用单个 map 实例创建多个显示不同标记的 mapbox-gl map