我正在尝试制作一种表格,用户可以搜索其位置或固定其位置。我react-leaflet
用于加载地图和react-leaflet-search
添加搜索功能。搜索功能运行正常。您可以在下面看到代码。
<Map center={position} zoom={zoom} onDragEnd = {function(e){ console.log(e);}} >
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url='https://{s}.tile.osm.org/{z}/{x}/{y}.png'/>
<Search
position="topright"
showPopup={false}
provider="OpenStreetMap"
showMarker={true}
openSearchOnLoad={true}
closeResultsOnClick={true}
providerOptions={{ region: "np" }}/>
</Map>
我要做的是访问用户输入的位置或用户选择位置后显示的标记的纬度经度。我试图搜索事件监听器,但找不到任何事件监听器。目前,我正在尝试使用onDragEnd
事件,但尚未成功。谁能告诉我如何实现我的目标?
不幸的是,react-leaflet-search没有适当的方法来检索搜索结果。有mapStateModifier
回调,我们可以用它来获得搜索结果坐标LatLng
对象,但我们必须要建立在地图flyTo
调用,以及:
render() {
const position = [51.505, -0.09];
const zoom = 13;
return (
<div>
<Map
ref={ref => this.mapRef = ref}
center={position}
zoom={zoom}
>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url='https://{s}.tile.osm.org/{z}/{x}/{y}.png' />
<ReactLeafletSearch
ref={ref => this.mapSearchRef = ref}
mapStateModifier={(latLng) => {
// Do work with result latitude, longitude
console.log('Search Latitude:', latLng.lat);
console.log('Search Longitude:', latLng.lng);
if (this.mapRef) {
// Because we have a custom mapStateModifier callback,
// search component won't flyTo coordinates
// so we need to do it using our refs
this.mapRef.contextValue.map.flyTo(
latLng,
this.mapSearchRef.props.zoom,
this.mapSearchRef.props.zoomPanOptions
);
}
}}
position="topright"
showPopup={false}
provider="OpenStreetMap"
showMarker={true}
openSearchOnLoad={true}
closeResultsOnClick={true}
providerOptions={{ region: "np" }}
/>
</Map>
</div>
);
}
您可以检查此示例Stackblitz以查看其工作情况。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句