gpt4 book ai didi

reactjs - React-leaflet 获取当前 latlng onClick

转载 作者:行者123 更新时间:2023-12-02 16:46:15 24 4
gpt4 key购买 nike

如果有人能帮助我,我会很高兴...我已经在我的 React 项目上安装了 React-leaflet 并且 map 组件已成功加载,我需要获取当前的经纬度并在单击 map 时在弹出窗口中显示它,但我不知道如何:(

请...请...帮助我...

这是我的代码

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;

最佳答案

以下是单击 map 后如何在弹出窗口中显示制造商位置的示例:

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 state 用于保持标记位置
  • event.latLng Map.onClick的属性(property)事件处理程序返回鼠标事件位置

Here is a demo供您引用

关于reactjs - React-leaflet 获取当前 latlng onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54503275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com