如何在使用 Leaflet 函数 getCurrentPosition 后自动居中我的位置?

How to auto center my position after function getCurrentPosition with Leaflet?

我创建了一个函数,它在地理定位后在地图上显示一个标记,但显示不会自动居中。 自动居中我的标记的参数是什么? 我认为这个问题出在 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)