我试图做一个表单,用户可以搜索他们的位置或锁定他的位置。我使用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事件,但尚未成功。有人能告诉我如何实现我正在努力做的事情吗?
发布于 2020-03-13 18:57:16
不幸的是,反应-传单-搜索没有一种检索搜索结果的正确方法。有mapStateModifier回调,我们可以使用它来获取搜索结果坐标LatLng对象,但是我们还必须设置map 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>
);
}您可以检查这个示例斯塔克布利茨,以确保它正常工作。
https://stackoverflow.com/questions/60340828
复制相似问题