Javascript 如何为传单地图上的每个标记元素分配唯一ID?

Javascript 如何为传单地图上的每个标记元素分配唯一ID?,javascript,leaflet,Javascript,Leaflet,我目前正在尝试将传单地图上的标记链接到地图左侧可用的部分文本。我想发生的是,当用户点击一个标记时,站点将滚动到该部分文本 我还应该注意,我的标记来自一个单独的geoJson文件 这是我打算在每个标记都有其唯一的ID进行操作后使用的代码: document.getElementById("A").addEventListener("click", function() { window.location.href = "#B"; })

我目前正在尝试将传单地图上的标记链接到地图左侧可用的部分文本。我想发生的是,当用户点击一个标记时,站点将滚动到该部分文本

我还应该注意,我的标记来自一个单独的geoJson文件

这是我打算在每个标记都有其唯一的ID进行操作后使用的代码:

document.getElementById("A").addEventListener("click", function() {
  window.location.href = "#B";
});
因此,在本例中,“A”是给其中一个标记的唯一ID,#B是我想通过href滚动到的文本的ID

*编辑

我应该澄清的是,主要问题实际上是给我添加到地图上的geoJson元素中的标记提供ID。 相关代码如下所示:

//convert points to circles in layer
function pointToLayer(feature, latlng, attributes){

    var attribute = attributes[0];

    // styling for the circles which are pink with white borders
    var geojsonMarkerOptions = {
        fillColor: "#800000",
        color: "white",
        weight: 1,
        opacity: 1,
        fillOpacity: 0.5,
    };

    var layer = L.circleMarker(latlng, geojsonMarkerOptions);

    //returns layer with circle markers
    return layer;

//function to fill the global attribute array variable at top of page
function processData(data){

    //variable to store the first feature in the data
    var properties = data.features[0].properties;

    //for loop to push each attribute name into array
    for (var attribute in properties){ 

        //indexOf will only allow attributes with population values to be pushed
        if (attribute.indexOf("Victims") > -1){
            attributes.push(attribute);

        };
    };

    return attributes; //returns attributes array to be used in callback
};


//function to retrieve the data from geojson
function getData(map){

    //load the data and calls functions
    $.getJSON("data/WarCrimes3.geojson");

};

您可以使用
uuid
模块,该模块将为您生成ID(通常用于安全应用程序),但是如果您想要一些简单的东西,您可以使用随机字符串,或者甚至使用当前的时间戳作为id。@SamridhTuladhar你知道我会以技术方式将随机字符串实际应用到每个元素吗?该链接将帮助你创建给定长度的随机字符串,我建议选择足够长的长度,以便两个随机生成的字符串,不要巧合地变得一样。e、 例如,如果我必须为100个元素分配唯一id,我会选择一个至少10-15个字符的随机字符串,以避免collision@SamridhTuladhar抱歉,我在编辑中澄清了问题在于如何将生成的ID分配给标记。您可以使用
uuid
模块,该模块将为您生成ID(这些通常用于安全应用程序),但如果您想要简单的东西,您可以使用随机字符串,甚至可以使用当前时间戳作为id。@SamridhTuladhar您知道我会以技术方式将随机字符串实际应用于每个元素吗?该链接将帮助您创建给定长度的随机字符串,我建议选择足够长的长度,这样两个随机生成的字符串就不会同时相同。例如,如果我必须为100个元素分配唯一id,我会选择一个至少10-15个字符大的随机字符串,以避免collision@SamridhTuladhar抱歉,我在编辑中澄清了问题在于如何将生成的ID分配给标记。