vue高德天气接口,加上自定义天气图片

Web端使用高德api获取天气 先获取城市,再通过城市获取天气

1.登录https://lbs.amap.com/,控制台->应用管理->我的应用->创建应用->添加key

*切记选择web端(JS API)

2.在index.html中 引入高德地图的css和js

 <!-- 高德地图js插件-->
  <script type="text/javascript">
    window._AMapSecurityConfig = {
      securityJsCode: '安全密钥jscode',
    }
  </script>
  <script type="text/javascript"
    src="https://webapi.amap.com/maps?v=1.4.15&key=key值&plugin=AMap.DistrictSearch,AMap.Geocoder,AMap.DistrictLayer,AMap.AutoComplete,AMap.PlaceSearch">

    </script>
3.在页面调用高德天气接口

        <div class="date">{{reportTime}}</div>

   <div class="weather">

          <span class="weather-pic"><img :src="imgUrl"></span>

          <span>{{weather}}</span>

          <span>{{temperature}}°C</span>

        </div>

data(){

return{

 weatherList: [

        { id: 1, name: "大雪", url: require("@/assets/icon/01.png") },

        { id: 2, name: "大雨", url: require("@/assets/icon/02.png") },

        { id: 3, name: "小雪", url: require("@/assets/icon/03.png") },

        { id: 4, name: "小雨", url: require("@/assets/icon/04.png") },

        { id: 5, name: "大暴雨", url: require("@/assets/icon/dabaoyu.png") },

        { id: 6, name: "多云", url: require("@/assets/icon/duoyun.png") },

        { id: 7, name: "雷阵雨", url: require("@/assets/icon/leizhenyu.png") },

        { id: 8, name: "霾", url: require("@/assets/icon/mai.png") },

        { id: 9, name: "晴", url: require("@/assets/icon/qing.png") },

        {id: 10,name: "沙尘暴",url: require("@/assets/icon/shachenbao.png"),},

        { id: 11, name: "雾", url: require("@/assets/icon/wu.png") },

        { id: 12, name: "扬尘", url: require("@/assets/icon/yangchen.png") },

        { id: 13, name: "阴", url: require("@/assets/icon/yin.png") },

        { id: 14, name: "雨夹雪", url: require("@/assets/icon/yujiaxue.png") },

        { id: 15, name: "阵雪", url: require("@/assets/icon/zhenxue.png") },

        { id: 16, name: "阵雨", url: require("@/assets/icon/zhenyu.png") },

      ],

      weather: "",

      temperature: "",

      imgUrl: "",

      reportTime: "",

}

getLngLatLocation() {

      const that = this;

      AMap.plugin("AMap.CitySearch", function () {

        var citySearch = new AMap.CitySearch();

        citySearch.getLocalCity(function (status, result) {

          if (status === "complete" && result.info === "OK") {

            AMap.plugin("AMap.Weather", function () {

              var weather = new AMap.Weather();

              // 执行实时天气信息查询

              weather.getLive(result.city, function (err, data) {

                console.log("天气", data);

                that.weather = data.weather;

                that.temperature = data.temperature;

                that.reportTime = data.reportTime;

                for (let i = 0; i < that.weatherList.length; i++) {

                  if (that.weatherList[i].name === data.weather) {

                    console.log(10101010,that.weatherList[i].name,data.weather)

                    that.imgUrl = that.weatherList[i].url;

                    console.log(99999999,that.imgUrl)

                    break;

                  }

                }

                // console.log(10101010,data)

              });

            });

          }

        });

      });

    },

  },

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值