react-leaflet 获取当前的经纬度 onClick

M.A*_*ish 8 leaflet reactjs react-leaflet

如果有人可以帮助我,我会非常高兴......我已经在我的反应项目中安装了反应传单并且地图组件已成功加载,我需要获取当前的纬度并在我单击地图时将其显示在弹出窗口中但我不知道怎么做:(

拜托……拜托……帮帮我……

这是我的代码

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;
Run Code Online (Sandbox Code Playgroud)

Vad*_*hev 11

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

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>
    )
  }
}
Run Code Online (Sandbox Code Playgroud)

解释:

  • currentPos 状态用于保持标记位置
  • event.latLngMap.onClick事件处理程序的属性返回鼠标事件位置

这是一个演示供您参考

  • 截至 2021 年 5 月,这不起作用,我想这是因为我使用的是 v3。在 v3 中如何做到这一点? (2认同)

Ale*_* V. 8

如果您使用react-leaflet 版本3.x,则这不起作用。在这种情况下,请在添加到地图的虚拟组件中使用 useMapEvents 挂钩。例如,如果您想 console.log 单击的位置:

const LocationFinderDummy = () => {
    const map = useMapEvents({
        click(e) {
            console.log(e.latlng);
        },
    });
    return null;
};
Run Code Online (Sandbox Code Playgroud)

然后在地图中使用LocationFinderDummy,如下所示:

<MapContainer
    center={[0, 0]}
    zoom={6}>
    <LocationFinderDummy />
</MapContainer>
Run Code Online (Sandbox Code Playgroud)


gre*_*tin 5

为了实现这个目标你做了什么尝试?

这将是开始:

使用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>
Run Code Online (Sandbox Code Playgroud)

在你的handleClick函数中,你可以得到纬度和经度的信息,如下所示:

handleClick = (e) => {
  const { lat, lng } = e.latlng;
  console.log(lat, lng);
}
Run Code Online (Sandbox Code Playgroud)

从这里开始,您可以使用您要查找的信息创建标记/弹出窗口。

附加提示:请确保您的代码在帖子中正确包装。