我想弄清楚如何手动触发Leaflet多边形的事件(通过GeoJSON加载)。如何在Leaflet地图多边形上触发事件?
简而言之,我有一个有多个多边形的Leaflet地图。我也有一个常规的超链接,当它被点击时,应该触发特定多边形上的鼠标悬停事件(或任何事件)。
如何为所有多边形分配ID以便我可以将超链接绑定到特定的多边形事件?或者,甚至是这样做的最合乎逻辑的方式?
最终,我试图创建与该关联到每一个多边形的文字标签的HTML表格沿无数多边形的地图。当点击HTML表格文本时,我想在地图多边形上触发事件(反之亦然)。我只是不知道如何引用每个多边形。
这是我非常简单的HTML:
<body>
<div id="map" style="height: 550px; width:940px"></div>
<a href="#" id="testlink">Click to trigger a specific polygon mouseover event</a>
</body>
这是我非常简单的JS:
$(document).ready(function() {
// build the map and polygon layer
function buildMap(data) {
var map = new L.Map('map');
var cloudmadeUrl = 'http://{s}.tile.cloudmade.com/***yourkeyhere***/66267/256/{z}/{x}/{y}.png',
cloudmadeAttribution = '',
cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18, attribution: cloudmadeAttribution});
var mapLoc = new L.LatLng(43.675198,-79.383287);
map.setView(mapLoc, 12).addLayer(cloudmade);
var geojsonLayer = new L.GeoJSON(null, {});
geojsonLayer.on("featureparse", function (e){
// apply the polygon style
e.layer.setStyle(polyStyle);
(function(layer, properties) {
layer.on("mouseover", function (e) {
// change the style to the hover version
layer.setStyle(polyHover);
});
});
layer.on("mouseout", function (e) {
// reverting the style back
layer.setStyle(polyStyle);
});
layer.on("click", function (e) {
// do something here like display a popup
console.log(e);
});
})(e.layer, e.properties);
});
map.addLayer(geojsonLayer);
geojsonLayer.addGeoJSON(myPolygons);
}
// bind the hyperlink to trigger event on specific polygon (by polygon ID?)
$('#testlink').click(function(){
// trigger a specific polygon mouseover event here
});
});
有没有办法做到这一点不钻研背景属性? – sidonaldson 2013-09-16 16:28:50
我相信这需要使用新的API进行更新,featureparse似乎不会触发,并且addGeoJSON似乎不是一个函数 – masterchief 2014-05-14 00:43:52
非常感谢你,我正在寻找类似的东西.. map._layers ['polyindex0' ] .fire( '点击'); – EvilInside 2014-08-04 21:10:56