1.引入百度地图js文件
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=4IU3oIAMpZhfWZsMu7xzqBBAf6vMHcoa"></script>
<script>
window.BMap = BMap;
</script>
2.文件中使用
var BMap = window.BMap;//取出window中的BMap对象
var myCity = new BMap.LocalCity();
let weatherLists = {};
myCity.get(function (result) {
if (result.name) {
axios.get ('http://free-api.heweather.com/v5/weather?key=19713447578c4afe8c12a351d46ea922', {
params: {
city: result.name
}
}).then(function (res) {
WeatherLists = res.data.HeWeather5[0];
let ress = require(`../../assets/weather/${weatherLists.now.cond.code}.png`).default
// setImgWeather(ress);(react 动态引入图片加载)
});
}
});
注:网址如果是https时,谷歌浏览器会出现阻止http接口使用,此时将http换成https即可(https://free-api.heweather.com/v5/weather?key=19713447578c4afe8c12a351d46ea922)