gpt4 book ai didi

javascript - 使用 leaflet onclick 事件创建动态链接

转载 作者:行者123 更新时间:2023-11-29 19:13:01 25 4
gpt4 key购买 nike

我在创建指向单独动态页面的链接时遇到问题,当用户单击由传单创建的标记时,用户会被定向到该页面。下面的代码几乎完全有效;但是,当单击标记而不是转到相关页面时,所有标记都链接到循环生成的最后一个链接


//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;
}

最佳答案

您必须创建一个关联数组并将每个数据的键保存在标记对象中。

// 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];

这是一个 example

关于javascript - 使用 leaflet onclick 事件创建动态链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37426809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com