如何解决如何在使用 Leaflet 函数 getCurrentPosition 后自动居中我的位置?
我创建了一个函数,一旦地理定位,它就会在地图上显示一个标记,并且显示不会自动居中。 自动居中标记的参数是什么? 我认为这个问题在 getCenter 上。
import { useState} from 'react';
import { useMap } from "react-leaflet";
import L from "leaflet";
import icon from "./iconPosition";
export default function LocationMarker() {
const map = useMap();
let [currentPosition,setCurrentPosition] = useState([0,0]);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
setCurrentPosition([position.coords.latitude,position.coords.longitude]);
});
let latlng = currentPosition;
L.marker(latlng,{ icon })
.addTo(map)
.bindPopup("Vous êtes ici")
.openPopup();
map.getCenter();
map.getZoom();
}
return (null)
};
解决方法
您需要设置中心,而不是获取当前中心。
替换
map.getCenter();
map.getZoom();
与
map.panTo(latlng)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。