【发布时间】:2014-04-14 19:50:14
【问题描述】:
我正在使用出色的插件Leaflet.Control.Search 在我的地图上搜索标记(来自 geoJson 标记组)——效果很好。
我现在只有一个简单的问题: 如何打开搜索结果标记的弹出窗口? 我正在使用已绑定弹出窗口(单击时打开)的自定义标记图标 - 但我想在通过搜索找到相应弹出窗口后自动打开它。
我的代码如下所示:
var searchControl = new L.Control.Search({layer: markers2, propertyName: 'Name', circleLocation:true});
searchControl.on('search_locationfound', function(e) {
e.layer.bindPopup(feature.properties.Name).openPopup();
}).on('search_collapsed', function(e) {
markers2.resetStyle(layer);
});
map.addControl( searchControl ); //inizialize search control
并认为它可能适用于该行:
e.layer.bindPopup(feature.properties.Name).openPopup();
但不幸的是它没有.. ;)
-
哦,还有第二个问题:目前我只搜索 1 个 geoJson 层 ("markers2") – 有人知道是否可以同时搜索多个层吗?
有什么建议吗?如有任何帮助,我将不胜感激,在此先感谢!
【问题讨论】:
-
你试过做e.layer.bindPopup(feature.properties.Name).addTo(map).openPopup();我认为你必须告诉它在哪里打开。所以像 addTo() 或 openOn() 这样的东西可能会起作用。检查文档here
-
嗨,克里斯,感谢您的指点!你的建议听起来不错,但它似乎并没有那样工作.. 嗯,在上面的搜索代码之前的某个时候,我已经定义了弹出内容,所以我想我不一定要为搜索弹出窗口,我只需要告诉它打开弹出窗口.. 但是如何?! ; )
-
弹出窗口的代码(在搜索之前)是这样的: var markers2 = new L.MarkerClusterGroup(); var geoJsonLayer2 = L.geoJson(data, { onEachFeature: function (feature, layer) { layer.bindPopup(feature.properties.Name); } ...
-
知道了:它的工作原理是这样的:e.layer.openPopup().openOn(map);