【发布时间】:2017-02-07 03:08:10
【问题描述】:
我正在尝试触发传单地图中的标记弹出窗口,但没有运气。我正在使用集群图,它可以正常工作并在用户单击标记时打开弹出窗口。我需要扩展这个,例如通过 url 传递参数并在页面加载时根据 url 参数值打开特定标记。我正在使用以下代码进行地图聚类。
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);
});
非常感谢您的帮助。提前致谢。
【问题讨论】:
-
参见 GIS SE 上的 Zoom to and Spiderfy MarkerClusterGroup, open popup of target marker(但省略了 spiderfy 部分)
-
感谢您的帮助。我已经尝试过了,但它不起作用。
codevar target = markers.getLayer(markers_array[id]) markers.zoomToShowLayer(target, function () { target.openPopup(); }) -
请确保您首先尽可能多地调试您的代码。编辑您的问题,而不是在评论中发布代码。如果可能,请在在线编辑工具上重现您的问题,使用SO built-in code snippet、Plunker、JSFiddle、JSBin 等。所有这些都将帮助您更快地获得支持,如果不能让您自己找到解决方案。
标签: javascript jquery google-maps leaflet markerclusterer