javascript - Mapbox:它渲染最后一个点而不是所有点

标签 javascript mapbox mapbox-gl-js

我正在尝试使用 Mapbox 渲染多个点,但它只显示最后一个点。我已经添加了功能参数,但什么也没有。这应该渲染两个标记,但我不知道为什么不渲染。我在控制台中没有收到任何错误。

我被困住了,找不到解决的方法。帮忙?

这是负责点的代码:

map.on('load', function() {
    map.loadImage('images/celltower.png', function(error, image) {
        if (error) throw error;
        map.addImage('tower', image);
        map.addLayer({
            "id": "points",
            "type": "symbol",
            "source": {
                "type": "geojson",
                "data": {
                    "type": "FeatureCollection",
                    "features": [{
                        "type": "Feature",
                        "geometry": {
                            "type": "Point",
                            "coordinates": [21.42559803007430, 42.00038270989050]
                        },
                        "geometry": {
                            "type": "Point",
                            "coordinates": [21.38529272846381, 42.0080397578202]
                        }
                    }]
                }
            },
            "layout": {
                "icon-image": "tower",
                "icon-size": 0.25
            }
        });
    });
});

谢谢:)

最佳答案

您有一个重复的geometry 键。从 features 是一个数组的事实来看,我猜这是正确的方法:

map.on('load', function() {
    map.loadImage('images/celltower.png', function(error, image) {
        if (error) throw error;
        map.addImage('tower', image);
        map.addLayer({
            "id": "points",
            "type": "symbol",
            "source": {
                "type": "geojson",
                "data": {
                    "type": "FeatureCollection",
                    "features": [{
                        "type": "Feature",
                        "geometry": {
                            "type": "Point",
                            "coordinates": [21.42559803007430, 42.00038270989050]
                        }
                    }, {
                        "type": "Feature",
                        "geometry": {
                            "type": "Point",
                            "coordinates": [21.38529272846381, 42.0080397578202]
                        }
                    }]
                }
            },
            "layout": {
                "icon-image": "tower",
                "icon-size": 0.25
            }
        });
    });
});

根据GeoJSON specification ,应该有一种方法来指定多个点。例如:

"features": [{
    "type": "Feature",
    "geometry": {
        "type": "MultiPoint",
        "coordinates": [
            [21.42559803007430, 42.00038270989050],
            [21.38529272846381, 42.0080397578202]
        ]
    }
}]

关于javascript - Mapbox:它渲染最后一个点而不是所有点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48470646/

相关文章:

reactjs - react Mapbox 层未显示

javascript - 提高 Mapbox GL 地理编码器的准确性

JavaScript 在 div 中加载后更改 HTML 代码

javascript - 跟踪 onclick 操作

javascript - 在javascript中遍历xml

swift - iOS 在 MapBox map 上移动注释

reactjs - 如何在弹出 map 框中使用 D3 图形?

javascript - 根据选择显示 div 并隐藏上一个 div

javascript - Mapbox GL JS 平铺有问题?

javascript - 如何在 React.js 中编写以下 Mapbox map 层?