如果有人能帮我的话我会很高兴的。我已经在我的react项目上安装了react-leaflet,并且地图组件加载成功,我需要获取最新的latlng,并在单击地图时在弹出窗口中显示它,但我不知道如何:(
请..。请..。帮帮我..。
这是我的代码
import React from 'react'
import { Map as LeafletMap, TileLayer, Marker, Popup } from 'react-leaflet';
class Mapp extends React.Component {
componentDidMount() {
}
render() {
return (
<LeafletMap
center={[35.755229,51.304470]}
zoom={16}
maxZoom={20}
attributionControl={true}
zoomControl={true}
doubleClickZoom={true}
scrollWheelZoom={true}
dragging={true}
animate={true}
easeLinearity={0.35}
>
<TileLayer
url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
/>
<Marker position={[35.755229,51.304470]}
draggable={true}
>
<Popup >
Popup for any custom information.
</Popup>
</Marker>
</LeafletMap>
);
}
}
export default Mapp;发布于 2019-02-04 04:26:11
以下是点击地图后如何在弹出窗口中显示maker位置的示例:
class MapExample extends Component {
constructor(props) {
super(props);
this.state = {
currentPos: null
};
this.handleClick = this.handleClick.bind(this);
}
handleClick(e){
this.setState({ currentPos: e.latlng });
}
render() {
return (
<div>
<Map center={this.props.center} zoom={this.props.zoom} onClick={this.handleClick}>
<TileLayer
url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
/>
{ this.state.currentPos && <Marker position={this.state.currentPos} draggable={true}>
<Popup position={this.state.currentPos}>
Current location: <pre>{JSON.stringify(this.state.currentPos, null, 2)}</pre>
</Popup>
</Marker>}
</Map>
</div>
)
}
}解释:
currentPos状态用于保持Map.onClick事件处理程序返回鼠标事件位置的标记position
event.latLng属性供您参考的Here is a demo
发布于 2019-02-03 23:02:28
您试图实现的目标是什么?
这将是开始:
使用LeafletMap组件中的click (请参阅https://leafletjs.com/reference-1.4.0.html#map-click)事件并调用您的函数,如下所示:
<LeafletMap
center={[35.755229,51.304470]}
zoom={16}
maxZoom={20}
attributionControl={true}
zoomControl={true}
doubleClickZoom={true}
scrollWheelZoom={true}
dragging={true}
animate={true}
easeLinearity={0.35}
onclick={this.handleClick}>
>
...
</LeafletMap>在您的handleClick函数中,您可以获得lat和lng的信息,如下所示:
handleClick = (e) => {
const { lat, lng } = e.latlng;
console.log(lat, lng);
}从这里开始,您可以使用您正在查找的信息创建您的标记/弹出窗口。
附加提示:请确保您的代码正确地包含在您的帖子中。
https://stackoverflow.com/questions/54503275
复制相似问题