Vue使用高德地图定位到当前位置,并显示天气信息

首先得去高德控制台申请两个 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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值