使用amap获取天气信息

引用amap

import VueAMap from 'vue-amap';
Vue.use(VueAMap);
VueAMap.initAMapApiLoader({
    key: '高德申请的开发者号',
    plugin: ['AMap.Weather','Amap.Geocoder','AMap.CitySearch'], // 天气
    v: '1.4.4',
    uiVersion: '1.0.11' // UI版本号
});

添加组件,使用城市定位,可以查看往期文章,获取ip定位

import { lazyAMapApiLoaderInstance } from 'vue-amap';
    export default {
        data () {
            return {
                weatherImg: require('@/assets/images/icon-weather/icon-3.png'),
                weatherObj: ''
            };
        },
        mounted () {
          let self = this;
          lazyAMapApiLoaderInstance.load().then(() => {
              AMap.plugin('AMap.CitySearch', function() {
                var citysearch = new AMap.CitySearch(); // 定位
                citysearch.getLocalCity((status, result) => {
                  if (status === 'complete' && result.info === 'OK') {
                    if (result && result.city && result.bounds) {
                      var cityinfo = result.city;
                      var weather = new AMap.Weather(); // 天气
                      weather.getLive(cityinfo, function(err, data) {
                        self.handleWeatherInfo(data);
                      });
                    }
                  }
                })
              });
          })

           // this.getCip();
        },
        computed: {

        },
        methods: {
            handleWeatherInfo(data) {
              switch (data.weather) {
                case '多云':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-3.png');
                  break;
                case '晴':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-4.png');
                  break;
                case '阴':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-1.png');
                  break;
                case '雷阵雨':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-11.png');
                  break;
                case '雨夹雪':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-9.png');
                  break;
                case '小雨':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-7.png');
                  break;
                case '小雪':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-6.png');
                  break;
                case '大雪':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-2.png');
                  break;
                case '雾':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-5.png');
                  break;
                case '浮尘':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-5.png');
                  break;
                case '沙尘暴':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-5.png');
                  break;
                case '中雪':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-8.png');
                  break;
                case '阵雪':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-10.png');
                  break;
              }
              this.curWeek =
                  '星期' + '日一二三四五六'.charAt(new Date(data.reporttime).getDay());
              this.curDay = new Date(data.reporttime).getDate();
              this.weatherObj = data;
              this.weatherObj.temperature_number = Number(this.weatherObj.temperature);
              this.$emit('getWeather', this.weatherObj);
            }
        }
   }

tips:如果不使用 AMap.plugin(‘AMap.CitySearch’, function() {});这一层的话,经常报错CitySerach不是一个构造函数,所以推荐使用这种方法

CitySearch由于网络问题无法获取到城市信息,尝试过好几种办法

1、通过外部script 获取ip地址,进行城市解析


          getCip () {
            let url = "https://www.taobao.com/help/getip.php";
            this.$jsonp(
                url,
                // 传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
                {
                  callbackName: 'ipCallback',
                  dataType: 'jsonp', // 数据类型为jsonp
                  jsonp: 'callback',
                  // 自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据
                  //  jsonpCallback: 'ipCallback',
                  type: 'GET',
                  async: false
                }
            ).then(res => {
              this.getCity(res.ip);
              // this.weather(res.adcode);
            });
          },
          getCity (cip) {
            this.$jsonp(
                'https://restapi.amap.com/v3/ip?ip=' +
                cip +
                '&output=json&key=高德地图开发者Key',
                // 传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
                {
                  dataType: 'jsonp', // 数据类型为jsonp
                  jsonp: 'callback',
                  // 自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据
                  // jsonpCallback: 'list',
                  type: 'GET',
                  async: false
                }
            ).then(res => {
              this.weather(res.adcode);
            });
          },

          weather (code) {
            this.$jsonp(
                'https://restapi.amap.com/v3/weather/weatherInfo?city=' +
                code +
                '&key=高德地图开发者Key',
                // 传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
                {
                  dataType: 'jsonp', // 数据类型为jsonp
                  jsonp: 'callback',
                  // 自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据
                  // jsonpCallback: 'list',
                  type: 'GET',
                  async: false
                }
            ).then(res => {
              var list = res.lives[0];
              switch (list.weather) {
                case '多云':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-3.png');
                  break;
                case '晴':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-4.png');
                  break;
                case '阴':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-1.png');
                  break;
                case '雷阵雨':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-11.png');
                  break;
                case '雨夹雪':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-9.png');
                  break;
                case '小雨':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-7.png');
                  break;
                case '小雪':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-6.png');
                  break;
                case '大雪':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-2.png');
                  break;
                case '雾':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-5.png');
                  break;
                case '浮尘':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-5.png');
                  break;
                case '沙尘暴':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-5.png');
                  break;
                case '中雪':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-8.png');
                  break;
                case '阵雪':
                  this.weatherImg = require('@/assets/images/icon-weather/icon-10.png');
                  break;
              }
              this.curWeek =
                  '星期' + '日一二三四五六'.charAt(new Date(list.reporttime).getDay());
              this.curDay = new Date(list.reporttime).getDate();
              this.weatherObj = list;
              this.weatherObj.temperature_number = Number(this.weatherObj.temperature);
              this.$emit('getWeather', this.weatherObj);
            });
          },

2、使用浏览器定位

// AMap.plugin('AMap.Geolocation', function() {
                      //   var geolocation = new AMap.Geolocation({
                      //     enableHighAccuracy: true, // 是否使用高精度定位,默认:true
                      //     timeout: 10000, // 超过10秒后停止定位,默认:5s
                      //     buttonPosition: 'RB', // 定位按钮的停靠位置
                      //     zoomToAccuracy: false // 定位成功后是否自动调整地图视野到定位点
                      //   });
                      //   geolocation.getCurrentPosition(function(status, result) {
                      //     if (status == 'complete') {
                      //       console.log(result);
                      //       // onComplete(result);
                      //     } else {
                      //       self.$Message.error("请先开启浏览器定位");
                      //     }
                      //   });
                      // });

第二种方法即使能获取到城市经纬度,js代码中报错,没处理……,写死经纬度就可以。目前不知道原因。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值