我正在嘗試獲取地圖地理編碼,但是在加載地圖時它從不使用新的緯度和經度。它始終是來自 useState 的原始版本。
const CustomMap = () => {
const [customLat, setCustomLat] = useState(0);
const [customLng, setCustomLng] = useState(0);
useEffect(() => {
Geocode.fromAddress("Eiffel Tower").then(
(response) => {
const { lat, lng } = response.results[0].geometry.location;
setCustomLat(lat);
setCustomLng(lng);
},
(error) => {
console.log(error.data);
}
);
}, [])
return (
<GoogleMapReact
bootstrapURLKeys={{ key: 'API_KEY' }}
defaultCenter={{ lat: customLat, lng: customLng }}
defaultZoom={11}>
</GoogleMapReact>
)
}
即使在控制臺中我可以看到它正在獲取帶有 Eifell 毛巾 url 的地圖。
Fetch finished loading: GET "https://maps.googleapis.com/maps/api/geocode/json?address=Eiffel Tower&key=KEY&language=en".
那是最后一個控制臺日志,但地圖的中心仍然是原始的。
uj5u.com熱心網友回復:
您使用的是默認值,這僅適用于第一次渲染,要更新您需要使用中心,像這樣
<GoogleMapReact
bootstrapURLKeys={{ key: 'API_KEY' }}
defaultCenter={{ lat: customLat, lng: customLng }}
center = {{lat: customLat, lng: customLng}}
defaultZoom={11}>
</GoogleMapReact>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/485249.html
