我刚刚发现了 Leaflet的OverlappingMarkerSpiderfier ,并认为它可能非常有用(请注意,Leaflet 版本提供的选项少于 Google 地图版本,但我仅限于 Leaflet)。
只需几分钟就可以将其添加到现有项目中,但我有一个小问题 :-(
当我单击这些中的任何标记时
我看到标记短暂扩展(蜘蛛化),但随后OnClick()
标记触发(将我移动到新状态,显示新页面,没有地图)。
我想我可以更改我的代码以不添加OnClick()
标记的处理程序,直到它展开,然后添加它,这样只有单击展开的(蜘蛛化)标记才会执行单击标记时当前执行的操作。
我的问题是这是否是普遍采用的方法。你是怎么做到的?
这是我的 AngularJs 代码,它在地图上添加了一个标记,以防万一。
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/** Given some data about a single conpany, add an appropriate marrker (and tooltip) to the map */
Self.AddCompanyMarkerToMap = function (companyData)
{
const companyName = companyData.company_name;
const latitude = companyData.latitude;
const longitude = companyData.longitude;
let iconImage = 'marker-icon-blue.png';
if (companyData['currentWorkers'] > 0)
iconImage = 'marker-icon-green.png';
//console.log('Add marker to map for company : ' + companyName);
Self.companyMarkers[companyName] = { lat: latitude, lng: longitude, message: companyName }
const markerLatLng = L.latLng(latitude, longitude);
const title = GetCompanyMapMarkerTitle(companyData);
// see https://leafletjs.com/reference-1.4.0.html#popup
////marker.bindPopup("<b>Popup for</b><br>" + companyName); // replaced by OnClick()
//console.log('Marker added to map for "' + companyName + '"');
// see https://leafletjs.com/reference-1.4.0.html#marker
const marker = L.marker(markerLatLng,
{
draggable: false,
// this is the tooltip hover stuff
title: title,
companyId: companyData['company_id'],
// see https://leafletjs.com/reference-1.4.0.html#icon
// this is a permanent label.
icon: new L.DivIcon({
//// className: cssClassname,
html: '<img class="my-div-image" src="../common/js/third_party/leaflet/images/' + iconImage + '" />'
+ '<span style="color:darkblue">' + companyName + '</span>',
className: 'dummy', // hide the square box See https://gis.stackexchange.com/questions/291901/leaflet-divicon-how-to-hide-the-default-square-shadow
iconSize: [41, 51], // size of the icon
iconAnchor: [20, 51], // point of the icon which will correspond to marker's location
popupAnchor: [0, -51] // point from which the popup should open relative to the iconAnchor
})
}).addTo(Self.map).on('click', Self.CompanyMarkerClicked);
Self.companyMarkers.push(marker);
Self.overlappingMarkerSpiderfier.addMarker(marker);
Self.UpdateMapBoundariesIfRequired(latitude, longitude);
}; // AddMarkerToMap()