【发布时间】:2016-03-13 20:32:21
【问题描述】:
This Mapbox tutorial 展示了如何构建列表并在单击列表项后将地图平移到地图标记处。
这是列表项根据特定标记处理其点击事件的方式:
// Iterate through each feature layer item, build a
// marker menu item and enable a click event that pans to + opens
// a marker that's associated to the marker item.
myLayer.eachLayer(function(marker) {
var link = info.appendChild(document.createElement('a'));
link.className = 'item';
link.href = '#';
// Populate content from each markers object.
link.innerHTML = marker.feature.properties.title +
'<br /><small>' + marker.feature.properties.description + '</small>';
link.onclick = function() {
if (/active/.test(this.className)) {
this.className = this.className.replace(/active/, '').replace(/\s\s*$/, '');
} else {
var siblings = info.getElementsByTagName('a');
for (var i = 0; i < siblings.length; i++) {
siblings[i].className = siblings[i].className
.replace(/active/, '').replace(/\s\s*$/, '');
};
this.className += ' active';
// When a menu item is clicked, animate the map to center
// its associated marker and open its popup.
map.panTo(marker.getLatLng());
marker.openPopup();
}
return false;
};
});
如何反过来呢?现在,如果您直接单击标记,则会出现弹出窗口,但列表项不会更新为所选标记。我不太确定如何将单击事件绑定到与特定列表项对应的地图标记。
【问题讨论】:
标签: javascript jquery geolocation leaflet mapbox