javascript Leaflet geosearch

您所在的位置:网站首页 leaflet自定义marker图标 javascript Leaflet geosearch

javascript Leaflet geosearch

2023-03-11 16:30| 来源: 网络整理| 查看: 265

我快速浏览了一下它们的源代码,该属性只用于提取其值,它实际上不会使用传入的示例,因此永远不会像您预期的那样使用。您的代码还有另一个问题。它是:

const searchControl = new GeoSearch.GeoSearchControl({ style: 'bar', provider: new GeoSearch.OpenStreetMapProvider ({ showMarker: true, marker: marker, // use custom marker, not working? }), });

但它应该是:

const searchControl = new GeoSearch.GeoSearchControl({ style: 'bar', provider: new GeoSearch.OpenStreetMapProvider (), showMarker: true, marker: marker, // use custom marker, not working? });

最后,为了解决您的问题,您可以改为侦听GeoSearch事件,更准确地说,是从结果列表中选择location时触发的showlocation事件,以清除先前的标记,因为该包将始终添加自己的标记(如果启用)。也就是说,在文件中添加以下代码应该可以解决这个问题。

map.on('geosearch/showlocation', () => { if (marker) { map.removeControl(marker); } });

根据您的评论更新

如果你想在使用GeoSearch库时保留初始标记属性,你必须自己应用它。

map.on('geosearch/showlocation', () => { if (marker) { map.removeControl(marker); } // The marker class extends the layer class, // so you can search for it among the layers map.eachLayer(item => { if (item instanceof L.Marker) { // Once you found it, set the properties item.options.draggable = true; item.options.autoPan = true; // Then enable the dragging. Without this, it wont work item.dragging.enable(); } }); });

Demo



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3