vue使用高德地图api获取当前位置天气预报

1.高德地图官网添加天气应用获取key和密钥

打开高德地图官网https://console.amap.com/ 从控制台中添加应用获取key和安全密钥
22

2.在vue项目public下的index.html中引入script

在公共文件下的index.html中配置你的key和安全密钥

<script type="text/javascript">
    window._AMapSecurityConfig = {
      securityJsCode: '安全密钥',
    }
  </script>
  <script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.1.js"></script>
  <script type="text/javascript"
    src="https://webapi.amap.com/maps?v=1.4.15&key=“你的key值”&plugin=(你需要使用的api)">
    </script>

3.使用api获取天气信息

调用获取当前位置api和天气api

//获取位置
    getCity() {
      let that = this
      if (window) {
        window.AMap.plugin("AMap.CitySearch", function () {
          var citySearch = new window.AMap.CitySearch();
          citySearch.getLocalCity(function (status, result) {
            if (status === "complete" && result.info === "OK") {
              // 查询成功,result即为当前所在城市信息
              that.getWeather(result.city)
            }
          });
        });
      }
    },
    // 获取天气
    getWeather(city) {
      let that = this
      // 加载天气查询插件
      window.AMap.plugin("AMap.Weather", function () {
        // 创建天气查询实例
        let weather = new window.AMap.Weather()
        // 执行实时天气信息查询
        weather.getLive(city, function (err, data) {
          if (data.info == "OK") {
            that.weatherdata = data
          }
        });
      });
    }
  • 21
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值