且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

传单:添加标记的链接

更新时间:2023-12-05 21:36:40

好吧,我终于找到解决方案了;当标记添加到地图时,它会被分配一个名为_leaflet_id"的 ID.这可以通过目标对象获取,也可以在将其添加到地图后设置为自定义值.

Okay, I finally came to a solution; when a marker is added to the map it gets assigned an ID called "_leaflet_id". This can be fetched through the target object, and also set to a custom value after it has been added to the map.

所以最终的解决方案很简单:

So the final solution is simply:

var x = markers.length;

while(x--)
{
    L.marker(markers[x].coords).on('click', function(e) {
        window.location = markers[e.target._leaflet_id].uri;
    }).addTo(map)._leaflet_id = x;
}

(我用反向的while循环替换了for-in循环)

(I replaced the for-in loop with a reversed while loop)