gpt4 book ai didi

javascript - 使用自定义链接单击传单 map 中的 openPopup()

转载 作者:行者123 更新时间:2023-12-03 05:04:12 24 4
gpt4 key购买 nike

我试图触发传单 map 中的标记弹出窗口,但没有运气。我正在使用聚类 map ,它工作正常,并在用户单击标记时打开弹出窗口。我需要扩展这个,例如通过url传递参数,页面加载时根据url参数值打开特定的标记。我正在使用以下代码进行 map 聚类。

        var latlng = L.latLng(-30.81881, 116.16596);
var map = L.map('lmap', { center: latlng, zoom: 6 });
var lcontrol = new L.control.layers();
var eb = new L.control.layers();


//clear map first
clearMap();
//resize the map
map.invalidateSize(true);
//load the map once all layers cleared
loadMap();
//reset the map size on dom ready
map.invalidateSize(true);
function loadMap() {

var markers_array = [];

var roadMutant = L.gridLayer.googleMutant({
type: 'roadmap' // valid values are 'roadmap', 'satellite', 'terrain' and 'hybrid'
}).addTo(map);


//add the control on the map

lcontrol= L.control.layers({
Roadmap: roadMutant

}, {}, {
collapsed: false
}).addTo(map);

var markers = L.markerClusterGroup({chunkedLoading: true, spiderfyOnMaxZoom: true, maxClusterRadius: 80, showCoverageOnHover: true });

//clear markers and remove all layers
markers.clearLayers();


$.ajax({
type: "GET",
url: appUrl + "/Home/map",
data: {'atype': st},
dataType: 'json',
contentType: 'application/x-www-form-urlencoded',
success: function (data) {

$.each(data, function (i, item) {
var img = (item.IconUrl).replace("~", "");
var Icon = L.icon({ iconUrl: img, iconSize: [42, 42] });

var marker = L.marker(L.latLng(item.Latitude, item.Longitude), { icon: Icon }, { title: item.Name });
var content = "<div class='infoDiv'><h3><img src='" + appUrl + img + "' width='24' />" + item.Name + "</h3><p>" + item.Title + "</p><a href='#' data-value='" + item.AlertId + "' class='btn btn-success btn-sm alertInfo' data-toggle='modal' data-target='#alertDetails'>Details</a></div>";
marker.bindPopup(content);
markers.addLayer(marker);
//add the marker to array
markers_array.push(marker);

});

}

})
.done(function () {
$(".loadingOverlay").hide();
map.invalidateSize(true);
});

//add the markers to the map
map.addLayer(markers);

}

我尝试实现以下自定义点击事件,但没有成功。

function markerFunction(id) {
alert(markers_array.length);

for (var i = 0; i < markers.length; ++i) {
var mid = markers_array[i]["_leaflet_id"];

if (mid == id) {
alert("opening " + id);
map.markers(id).openPopup();

}
}
}
//trigger on link click
$("a").click(function () {
var id = $(this).attr("id");
alert(id);
markerFunction(id);

});

非常感谢您的帮助。提前致谢。

最佳答案

loadMap() 异步获取其数据。任何处理该数据(或从该数据派生的任何内容)的方式都必须考虑异步性,通常是在链式 .then() 中。

就目前情况而言,标记是异步创建的,但单击处理程序是独立定义和附加的。通过从 loadMap() 返回的 Promise 传递 markers_array (和 markers?) 将允许在以下时刻完全填充必要的标记数据:附件并带入点击处理程序的范围。

我会写这样的东西:

var latlng = L.latLng(-30.81881, 116.16596);
var map = L.map('lmap', { center: latlng, zoom: 6 });
var lcontrol = new L.control.layers(); // necessary?
var eb = new L.control.layers(); // necessary?

clearMap(); // why, it's only just been created?
map.invalidateSize(true);
loadMap(map).then(function(obj) {
$(".loadingOverlay").hide();
map.invalidateSize(true); // again?

$("a").click(function(e) { // jQuery selector probably needs to be more specific
e.preventDefault();
var id = $(this).attr('id');
for(var i=0; i<obj.markers_array.length; ++i) {
if(obj.markers_array[i]._leaflet_id == id) {
map.markers(id).openPopup(); // if `map.markers` is correct, maybe you don't need obj.markers?
break; // break out of `for` loop on success.
}
}
});
return obj;
});

function loadMap(map) {
var roadMutant = L.gridLayer.googleMutant({ type: 'roadmap' }).addTo(map);
var lcontrol = L.control.layers({Roadmap: roadMutant}, {}, {collapsed: false}).addTo(map);

return $.ajax({
type: 'GET',
url: appUrl + '/Home/map',
data: {'atype': st},
dataType: 'json',
contentType: 'application/x-www-form-urlencoded'
}).then(function (data) {
var markers = L.markerClusterGroup({chunkedLoading: true, spiderfyOnMaxZoom: true, maxClusterRadius: 80, showCoverageOnHover: true });
markers.clearLayers();
var markers_array = $.map(data, function(item) {
var img = (item.IconUrl).replace("~", "");
var Icon = L.icon({ iconUrl: img, iconSize: [42, 42] });
var marker = L.marker(L.latLng(item.Latitude, item.Longitude), { icon: Icon }, { title: item.Name });
var content = "<div class='infoDiv'><h3><img src='" + appUrl + img + "' width='24' />" + item.Name + "</h3><p>" + item.Title + "</p><a href='#' data-value='" + item.AlertId + "' class='btn btn-success btn-sm alertInfo' data-toggle='modal' data-target='#alertDetails'>Details</a></div>";
marker.bindPopup(content);
markers.addLayer(marker);
return marker;
});
map.addLayer(markers); //add the markers to the map
// If both 'markers_array' and 'markers' are needed later, then bundle them into an object.
// If not, then simply return one or other of those variables.
return {
'markers_array': markers_array,
'markers': markers
};
});
}

细节需要检查,但整体模式应该是正确的。

关于javascript - 使用自定义链接单击传单 map 中的 openPopup(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42081012/

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