- 在public文件夹下的index.html文件中移入如下代码
<script src="http://webapi.amap.com/maps?v=1.4.6&key=申请的key"></script>
- 直接使用AMap会报错,可以使其成为全局
const AMap = window.AMap
- 实现定位和获取天气信息
getCity = () => {
AMap.plugin('AMap.CitySearch', () => {
const citySearch = new AMap.CitySearch()
citySearch.getLocalCity((status, result) =>{
if (status === 'complete' && result.info === 'OK') {
this.getWeather(result.city)
}
})
})
}
// 获取天气信息
getWeather = (city) => {
//加载天气查询插件
AMap.plugin('AMap.Weather', () => {
//创建天气查询实例
var weather = new AMap.Weather();
//执行实时天气信息查询
weather.getLive(city, (err, data) => {
this.setState({
weatherData: data.weather,
temperature: data.temperature
})
});
});
}
- 在dom渲染完成前的生命周期函数中使用
componentWillMount () {
this.getCity()
}