react使用高德定位怎么在其方法中获得this
React使用高德插件定位,采用的是同步插入,没有使用高德推荐的模块,采用类式组件。使用插件的过程中,想要得到定位的所在地名字,成功获得,但是将要保存到state中时,确发现没有this,还望指点。
getCurrentCity=()=>{
window.AMap.plugin('AMap.CitySearch', function () {
var citySearch = new window.AMap.CitySearch()
citySearch.getLocalCity(function (status, result) {
if (status === 'complete' && result.info === 'OK') {
// 查询成功,result即为当前所在城市信息
// this.setState({
// currentCity:result.city
// })
console.log(result.city)
}
})
})
}
运行结果及详细报错内容
无this,怎么找到this,然后使用setState方法
我的解答思路和尝试过的方法
试过传递this进去,发现压根
我想要达到的结果
找到this,在if判断语句中能够使用this.setState保存获取的数据