javascript - leaflet JS Cluster Maker 捕获 ids child

标签 javascript jquery ajax leaflet

我正在使用 leaflet.js 作为标记聚类 map ,我需要获取聚类中这些点的 id 来创建 ajax 查询,我的代码工作正常,但在聚类时无法获取 id如果您可以帮助我,请在这个字段中 e.layer.options.myCustomId 中获取非集群时的 id。感恩!

var tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        maxZoom: 18,
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }),
    //latlng = L.latLng(-22.01, -47.89);
    latlng = L.latLng(<?php echo $localizacao['lat']; ?> , <?php echo $localizacao['lng']; ?>);

var map = L.map('map', {center: latlng, zoom: 13, layers: [tiles]});

var markers = L.markerClusterGroup({spiderfyOnMaxZoom: false, showCoverageOnHover: false, zoomToBoundsOnClick: false});
function populatephp(){
    <?php foreach ($endereco_id as $key => $value) { ?>
        var muxiCoordinates = [<?php echo $value['lat']; ?>, <?php echo $value['lng']; ?>];
        var muxiMarkerMessage = "<img src='<?php echo $value['img']; ?>'><br><?php echo $value['dados']; ?> ";
        var id = "<?php echo $value['id']; ?>"
        var marker = L.marker(muxiCoordinates, {myCustomId: id})
        .bindPopup(muxiMarkerMessage);
        markers.addLayer(marker);
    <?php } ?>
    return false;

}

markers.on('clusterclick', function (a) {
    a.layer.zoomToBounds();
});

markers.on('mouseover', mudarElementos);
populatephp();
map.addLayer(markers);

function markerOnOver(e){
    e.layer.openPopup();
}
function markerOnOut(e){
    setTimeout(function(){ e.layer.closePopup(); }, 9000);
}

function mudarElementos(e){
    inverterPosicoes("#corpo_pesquisa", "#myCard_" + e.layer.options.myCustomId)
    //console.log(e.layer.options.myCustomId);

}

最佳答案

对于那些需要一天和 future 咨询的人,请遵循我如何解决我的问题。

markers.on('clusterclick', function (a) {
        var ids = []; 
        var markers = a.layer.getAllChildMarkers();
        for (var i = 0; i < markers.length; i++) {
            var id = markers[i].options.myCustomId;
            ids.push(id);
        }
        console.log(ids);
        a.layer.zoomToBounds();
    });

关于javascript - leaflet JS Cluster Maker 捕获 ids child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58430991/

相关文章:

JQuery、XmlHttpRequest 和状态代码 0

javascript - NodeJS - 如何在没有框架的情况下解析多部分表单数据?

javascript - FileList 和 File[](又名文件数组)之间有什么区别

javascript - 使用 native 或未知堆栈帧位置创建错误

javascript - 你如何只显示一次 JS 警报?

javascript - 共享自定义元素创建性能

javascript - $ slider .css( {'transform' : 'translateY(' + (-$height * $counter) + 'px)' })

c# - 使用 jquery 和代码隐藏更新标签

php - Jquery、php ajax post 500 内部服务器错误返回

ajax - 使用HTTP2如何限制并发请求数?