首先得去高德控制台申请两个 key,一个天气key和一个定位key
获取天气信息的函数:
const getWeather = function (city) {
// 使用 fetch 发送请求获取天气信息
fetch(`https://restapi.amap.com/v3/weather/weatherInfo?city=${city}&key=eefd36557b0250d19d243aea0f62d499`)
.then((response) => response.json())
.then((data) => {
const { lives } = data;
// 更新响应式数据
weather.city = lives[0].city;
weather.weather = lives[0].weather;
weather.temperature = lives[0].temperature;
weather.winddirection = lives[0].winddirection;
weather.windpower = lives[0].windpower;
weather.reporttime = lives[0].reporttime;
});
};
使用Geolocation API获取当前位置:
onMounted(() => {
let latitude = 0;
let longitude = 0;
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(
func