//监听鼠标点击事件,单击弹出经纬度,双击获取坐标
useEffect(() => {
const map = mapRef.current.leafletElement;
function onMapClick(e) {
L.popup()
.setLatLng(e.latlng)
.setContent(
`经纬度:<span id= 'copylatlng'>${e.latlng.lng},${e.latlng.lat}
</span>`
)
.openOn(map);
}
const copy = () => {
let range = document.createRange();
setTimeout(() => {
window.getSelection().removeAllRanges();
// console.log(typeof(document.getElementById("copylatlng")), document.getElementById('copylatlng'))
if (document.getElementById("copylatlng")) {
range.selectNode(document.getElementById("copylatlng"));
window.getSelection().addRange(range);
const res = document.execCommand("copy");
if (res) {
message.success("坐标已复制成功!");
} else {
message.warning("复制失败,请手动复制!");
}
window.getSelection().removeAllRanges();
}
}, 200);
};
map.on("click", onMapClick);
map.on("dblclick", copy);
}, [mapRef]);
双击复制坐标
最新推荐文章于 2023-07-17 17:28:28 发布