【问题标题】:Leaflet Control Search: open Popup for search resultLeaflet Control Search:打开搜索结果的弹出窗口
【发布时间】: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);

标签: search popup leaflet


【解决方案1】:

知道了:它的工作原理是这样的:e.layer.openPopup().openOn(map);

【讨论】:

  • 至于我的第二个问题,有人给我提示吗?目前我只在 1 个 geoJson 层中搜索 L.Control.Search({layer: markers2, propertyName… - 有谁知道是否可以一次在多个层中搜索?我是这样尝试的:{layer: (markers1, markers2), propertyName… 这不起作用……
【解决方案2】:

event.layer 仅针对预加载层设置,如果您通过 ajax、jsonp 或 callData 搜索标记.. event.layer 未定义。

var geojsonLayer = new L.GeoJSON(data, {
        onEachFeature: function(feature, marker) {
            marker.bindPopup(feature.properties.name);
        }
    });

map.addLayer(geojsonLayer);

var controlSearch = new L.Control.Search({layer: geojsonLayer, initial: false});

controlSearch.on('search_locationfound', function(event) {
    event.layer.openPopup();
});

查看 GeoJSON 演示: https://opengeo.tech/maps/leaflet-search/examples/geojson-layer.html

【讨论】:

    猜你喜欢
    • 2014-10-07
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多