react使用高德进行定位
-
在public文件夹下的index.html文件中引入
<script src="http://webapi.amap.com/maps?v=1.4.6&key=申请的key"></script>
-
直接使用AMap会报错所以要在当前文件内全局引入
const AMap = window.AMap
-
实现定位
state={ city:'' } //城市定位 getCity = () => { AMap.plugin('AMap.CitySearch', () => { const citySearch = new AMap.CitySearch() citySearch.getLocalCity((status, result) => { console.log(result.city); this.setState({ city:result.city }) }) }) }
<div>城市:{this.state.city}</div>