【发布时间】:2021-02-19 23:43:13
【问题描述】:
我正在使用传单库处理 react.js,我希望在单击地图时移动标记而不添加新标记。这是我的代码:
import React from "react";
import {
MapContainer,
TileLayer,
useMapEvents,
MapConsumer
} from "react-leaflet";
import "leaflet/dist/leaflet.css";
import L from "leaflet";
import icon from "./constants";
import "./styles.css";
export default function App() {
function MyComponent() {
const map = useMapEvents({
click: (e) => {
const { lat, lng } = e.latlng;
L.marker([lat, lng], { icon }).addTo(map);
}
});
return null;
}
return (
<MapContainer
center={[50.5, 30.5]}
zoom={13}
style={{ height: "100vh" }}
// whenReady={(map) => {
// console.log(map);
// map.target.on("click", function (e) {
// const { lat, lng } = e.latlng;
// L.marker([lat, lng], { icon }).addTo(map.target);
// });
// }}
>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<MapConsumer>
{(map) => {
console.log("map center:", map.getCenter());
map.on("click", function (e) {
const { lat, lng } = e.latlng;
L.marker([lat, lng], { icon }).addTo(map);
});
return null;
}}
</MapConsumer>
</MapContainer>
);
}
你可以在那里找到我的代码:my code
你能帮帮我吗?
非常感谢!
【问题讨论】:
标签: javascript reactjs leaflet react-leaflet