简体   繁体   English

在反应传单中获取标记的纬度和经度

[英]Getting latitude and longitude of a marker in react-leaflet

I'm trying to get the latitude and longitude of a marker in react-leaflet, but can't seem to figure out how.我试图在 react-leaflet 中获取标记的纬度和经度,但似乎无法弄清楚如何。 In Leaflet, there is a function for it but I can't figure out how to call that in react-leaflet. 在 Leaflet 中,有一个函数,但我不知道如何在 react-leaflet 中调用它。

My goal is to have a marker on the map (fixed location) and another draggable one.我的目标是在地图上有一个标记(固定位置)和另一个可拖动的标记。 When you drag the marker, it will print the coordinates to console onDragEnd and then tell you the distance between the two.当您拖动标记时,它会将坐标打印到控制台 onDragEnd,然后告诉您两者之间的距离。 I've been reading the documentation and looking at the code, but can't seem to implement this in my React component.我一直在阅读文档并查看代码,但似乎无法在我的 React 组件中实现这一点。

Any help is greatly appreciated.任何帮助是极大的赞赏。

As per the react-leaflet component docs:根据 react-leaflet 组件文档:

You can directly access the Leaflet element created by a component using this.leafletElement in the component.您可以在组件中使用 this.leafletElement 直接访问由组件创建的 Leaflet 元素。 This leaflet element is usually created in componentWillMount() and therefore accessible in componentDidMount(), except for the Map component where it can only be created after the container is rendered.这个传单元素通常在 componentWillMount() 中创建,因此可以在 componentDidMount() 中访问,除了 Map 组件,它只能在容器渲染后创建。

Components成分


Here's a quick minimal example which assigns a ref to the marker and watches for the dragEnd event.这是一个快速的最小示例,它为标记分配一个refdragEnd事件。

React-Leaflet example 反应传单示例

 .leaflet-container { height: 70vh; width: 100vw; }
 <script src="https://unpkg.com/react@16/umd/react.production.min.js"></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" /> <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"></script> <script src="https://unpkg.com/react-leaflet/dist/react-leaflet.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> <script type="text/babel"> const { useState, useRef, useEffect } = React; const { Map, TileLayer, Marker } = window.ReactLeaflet function MapComp() { const [markerPos, setMarkerPos] = useState({ lat: 55.702868, lng: 37.530865, }) const [fixedMarkerPos, setFixedMarkerPos] = useState({ lat: 55.7518300742972, lng: 37.71057128906251, }) useEffect(() => { console.log(`lat diff: ${markerPos.lat - fixedMarkerPos.lat}, lng diff: ${markerPos.lng - fixedMarkerPos.lng}`); },[markerPos, fixedMarkerPos]) const markerRef = useRef(); const fixedMarkerRef = useRef(); const updatePosition = () => { const marker = markerRef.current if (marker != null) { const newPos = {...marker.leafletElement.getLatLng()}; setMarkerPos(newPos); } } return ( <Map zoom={9} center={[55.74101998457737, 37.62268066406251]}> <TileLayer attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" /> <Marker position={[markerPos.lat, markerPos.lng]} draggable={true} onDragend={updatePosition} ref={markerRef} /> <Marker position={[fixedMarkerPos.lat, fixedMarkerPos.lng]} ref={fixedMarkerRef} /> </Map> ); } ReactDOM.render(<MapComp />, document.getElementById('root')); </script> <div id="root"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM