1. main.js
import VueAMap from 'vue-amap'
Vue.use(VueAMap)
VueAMap.initAMapApiLoader({
key: 'key值',
plugin: [
'AMap.Autocomplete',
'AMap.PlaceSearch',
'AMap.Scale',
'AMap.OverView',
'AMap.ToolBar',
'AMap.MapType',
'AMap.PolyEditor',
'AMap.CircleEditor',
'AMap.Geolocation', // 定位
'AMap.Weather', // 天气
'AMap.CitySearch' // 城市
],
// 默认高德 sdk 版本为 1.4.4
v: '1.4.4'
})
2. 组件里
<script>
import { lazyAMapApiLoaderInstance } from 'vue-amap'; //
export default {
mounted(){
lazyAMapApiLoaderInstance.load().then(() => {
var citysearch = new AMap.CitySearch(); // 定位
citysearch.getLocalCity(function(status, result) {
if (status === 'complete' && result.info === 'OK') {
if (result && result.city && result.bounds) {
var cityinfo = result.city;
console.log(cityinfo)
var weather = new AMap.Weather(); // 天气
weather.getLive(cityinfo, function(err, data) {
console.log(err, data.weather);
});
}
}
})
})
}
}
</script>