【问题标题】:How to get the markers details in the drawn polygon?如何获取绘制多边形中的标记细节?
【发布时间】:2020-06-29 23:51:45
【问题描述】:

标记将使用 firebase 动态添加。

map.loadImage(
AddressIcon,
function(error, image) {
    if (error) throw error;
    map.addImage(id + 'address', image);
    map.addSource(id + 'point', {
        'type': 'geojson',
        'data': {
            'type': 'FeatureCollection',
            'features': [
                features
            ]
        }
    });
    map.addLayer({
        'id': id + "addresses-layer",
        'type': 'symbol',
        'source': id + 'point',
        'layout': {
            'icon-image': id + 'address',
            'icon-size': 1
        }
    });
});

draw = new MapboxDraw({
        displayControlsDefault: false,
        userProperties: true,
        controls: {
            polygon: true,
            trash: true
        },
    });

map.addControl(draw, 'bottom-left');
map.addControl(new mapboxgl.NavigationControl());
map.addControl(new mapboxgl.FullscreenControl());

map.on('draw.create', updateDrawArea);
map.on('draw.delete', updateDrawArea);
map.on('draw.update', updateDrawArea);

const updateDrawArea = (e) => {
    var data = draw.getAll();
    console.log(data);
}

我在地图上有多边形绘制系统。在标记周围绘制多边形后,我需要获取所有添加的图层/标记。如果可能的话,我需要使用 mapbox-gl-js 来做到这一点。如果没有,还有其他选择吗?

【问题讨论】:

    标签: javascript reactjs mapbox mapbox-gl-js mapbox-marker


    【解决方案1】:

    是的,可以通过Turf.js

    Turf.js 公开了诸如pointsWithinPolygon 之类的函数,允许我们指定marker pointspolygon 坐标,并返回指定多边形内的标记列表。

    例如:

    var pointsWithinPolygon = require("@turf/points-within-polygon")
    const turf = require('@turf/turf');
    
    var points = turf.points([
        [-46.6318, -23.5523],
        [-46.6246, -23.5325],
        [-46.6062, -23.5513],
        [-46.663, -23.554],
        [-46.643, -23.557]
    ]);
    
    var searchWithin = turf.polygon([[
        [-46.653,-23.543],
        [-46.634,-23.5346],
        [-46.613,-23.543],
        [-46.614,-23.559],
        [-46.631,-23.567],
        [-46.653,-23.560],
        [-46.653,-23.543]
    ]]);
    
    var ptsWithin = turf.pointsWithinPolygon(points, searchWithin);
    console.log(ptsWithin)
    

    您可以参考this 教程,该教程解释了我们如何将 turf.js 与 mapbox-gl-draw 一起使用。

    【讨论】:

    • 感谢您的回答。我想我无法正确描述。我需要绘制多边形内的所有标记。 prnt.sc/t9g44p 请参考这张图片我已经更新了你的小提琴来描述你。
    • @TareqAziz 我已根据您的说明更新了我的答案。我建议使用此说明更新问题,以便该帖子的未来访问者获得正确的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 2018-07-24
    • 1970-01-01
    相关资源
    最近更新 更多