我有以下功能通过使用纬度和经度的 API 检索城市:
export const GeoLocation = async () => {
const geolocation = useGeolocation();
const latitude = geolocation.latitude;
const longitude = geolocation.longitude
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${API_KEY}`;
const res = await axios.get(url);
let setCityLocation = {
city: res.data.name,
}
console.log(setCityLocation);
}
如何将恢复的城市名称传递给以下语句:
const [city, setCity] = useState('Turi');
我必须输入地理定位的位置,而不是显示“Turi”的城市。
我有所有这些特征,其中城市常数被称为:
const handleCityWeather = () => {
getCityWeather(city)
.then((setData) => {
setWeather(setData);
setIsHeartSelected(false);
setLoading(false);
})
.catch((error) => {
setError(true);
})
}
const handleForeCast = (city) => {
getCityForecast(city)
.then((forecast) => {
setForecast(forecast);
setError(false);
})
.catch((error) => {
setError(true);
})
}
useEffect(() => {
if (!city) {
return;
}
handleCityWeather(city)
}, [city, isError])
useEffect(() => {
if (!city) {
return;
}
handleForeCast(city)
}, [city, isError])
const debouncedSearchTerm = useDebounce((value) => setCity(value), delay);
const onInputChange = (value) => debouncedSearchTerm(value);
const getSearchWeather = (event) => {
event.preventDefault();
getCityWeather(city);
getCityForecast(city);
}
我应该改变什么值?