简体   繁体   English

使用传单onclick事件创建动态链接

[英]Creating dynamic links with leaflet onclick event

Im having trouble creating links to a separate dynamic page, where the user is directed to the page when they click a marker created by leaflet. 我在创建指向单独的动态页面的链接时遇到了麻烦,当用户单击由传单创建的标记时,会将用户定向到该页面。 The code below almost entirely works; 下面的代码几乎完全可以工作; however, when the marker is clicked instead of going to the relevant page, all the markers link to the last link generated by the loop 但是,当单击标记而不是转到相关页面时,所有标记都链接到循环生成的最后一个链接


//Add a marker to the map on the results page for each result
function Addmarker(markerArray){

    var dynamicname = 'marker';
    var dynamicnumb = 'numb';
    //create an empty list to hold each marker
    var markerList = [];
    //For each result creater a marker and push it to markerList
    for (i = 0; i < markerArray.length; i++) {
        //Turn the park id into an integer
        var toInt = parseInt(markerArray[i][2]);
        //Generate the marker in the form L.marker(Latitude, Longitude, Name of Park to be shown on mouse over, link to the individual item page on click).add the marker to mymap
        this[dynamicname+i] = L.marker([markerArray[i][0], markerArray[i][1]], {title: markerArray[i][3]}).on('click', function(e) {markerURL(toInt);}).addTo(mymap);
        //Place the marker in a list
        markerList.push(this[dynamicname+i]);
    }
    //Create a feature group of the markers in markerList
    var group = new L.featureGroup(markerList);
    //Auto scale the map to fit all the markers perfectly
    mymap.fitBounds(group.getBounds().pad(0.2));
}

//create a dynimic link to the individual items page specified in the parameter itemsID
function markerURL(itemsID){
    window.location.href = 'Items.php?parkid=' + itemsID;
}

You must create an associative array and keep the key of each data in the marker object. 您必须创建一个关联数组,并将每个数据的键保留在标记对象中。

// generate a unique id
var toInt = parseInt(markerArray[i][2]);

// create marker object, add it to the map
var marker = L.marker([markerArray[i][0], markerArray[i][1]], {
  title: markerArray[i][3]}).on('click', function(e) {
     markerURL(e.target.ID); // url = markerList[e.target.ID][3];
  }).addTo(map);

// keep the unique id in the marker object  
marker.ID = toInt;

// create an item in the associative array
markerList[toInt] = markerArray[i];

Here is an example 这是一个例子

Closures, closures, closures. 封闭,封闭,封闭。

function Addmarker(markerArray){
    for (i = 0; i < markerArray.length; i++) {
        var toInt = parseInt(markerArray[i][2]);

        this[dynamicname+i] = L.marker(
            [markerArray[i][0], markerArray[i][1]], 
            {title: markerArray[i][3]}
        ).on('click', onMarkerClick(toInt)).addTo(mymap);

}

function onMarkerClick(itemsID){
    return function(ev) {
        window.location.href = 'Items.php?parkid=' + itemsID;
    }
}

See also Leaflet marker event fires at wrong time , which is pretty much a duplicate of your question. 另请参见在错误的时间触发传单标记事件 ,这几乎与您的问题重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM