1.逆向地理编码(将经度纬度转换地址)时报错10009(USERKEY_PLAT_NOMATCH),明明用的是web端的key值,为什么会报不匹配的bug呢?
可能的原因是:在逆向地理编码的时候用的不是逆向编码的url,正确的url地址如图:
2.写地理编码时报错:AMap.Geocoder is not a constructor,网上查到的解决方法有:
1)入口index.html文件的引入的script的key值后面加上plugin=AMap.Geocoder
2)或者是在key值后面接&callback=init
我试了这两种方法都没起作用,想到有可能是调用逆地址转码时GeoCoder实例还没加载完成,所以在MapDemo组件中用了以下方法得以解决:
// 实例点击事件
mapInstance.on('click', e => {
const lngLat = `${e.lnglat.getLat()},${e.lnglat.getLng()}`
console.log('坐标位置:', lngLat);
const self = this;
AMap.plugin('AMap.Geocoder', function() { //eslint-disable-line
const geocoder = new AMap.Geocoder({});//eslint-disable-line
geocoder.getAddress([e.lnglat.lng, e.lnglat.lat], (status, result) => {
console.log('status, result:', status, result);
if (status === 'complete' && result.info === 'OK') {
console.log(result.regeocode.formattedAddress);
self.setState({inputValue: result.regeocode.formattedAddress})
}
})
})
});