Web端使用高德api获取天气
先获取城市,再通过城市获取天气
1.登录https://lbs.amap.com/,控制台->应用管理->我的应用->创建应用->添加key
*切记选择web端(JS API)
2.在index.html中添加
3.vue文件里面
getLocation() {
var that=this
AMap.plugin('AMap.CitySearch', function () {
var citySearch = new AMap.CitySearch()
citySearch.getLocalCity(function (status, result) {
console.log(result)
if (status === 'complete' && result.info === 'OK') {
// 查询成功,result即为当前所在城市信息
that.city=result.city
AMap.plugin('AMap.Weather', function() {
//创建天气查询实例
var weather = new AMap.Weather();
//执行实时天气信息查询
weather.getLive(result.city, function(err, data) {
console.log(err, data);
that.number=data.temperature
that.weather=data.weather
that.day=data.reportTime.split(' ')[0]
});
});
}
})
})
}