AMap.Geocoder is not a constructor
按坑爹的高德這個實例代碼,報這個錯誤。
在前面需要加載AMap.Geocoder,如下代碼
AMap.service("AMap.Geocoder", function(){
var geocoder = new AMap.Geocoder({
radius: 1000,
extensions: "all",
city: "010"
});
geocoder.getAddress([116.396574, 39.992706], function(status, result) {
if (status === 'complete' && result.info === 'OK') {
console.log(result)
}
});
})
這樣就加載了Geocoder
(這個絕非水貼, 百度前十頁絕對沒有答案~~,有也是回答錯誤。)
--問題解決--

然后發(fā)現(xiàn)并無法走入回掉。
在ok之前打印result, 發(fā)現(xiàn)值是USERKEY_PLAT_NOMATCH
這個錯誤倒是可以百度到, 是錯誤代碼。
這個功能屬于 Web端, 不是web服務,重新生成key,即可
錯誤信息列表
http://lbs.amap.com/api/javascript-api/reference/errorcode/
話說都寫到這里了,我也水一下, 附上完整定位函數(shù)代碼
gpsData: function(data){
return data || false
},
getGps: function(callback){
if(this.gpsData()){
return this.gpsData()
}
let self = this
var map, geolocation;
//加載地圖,調(diào)用瀏覽器定位服務
map = new AMap.Map('container', {
resizeEnable: true
});
map.plugin('AMap.Geolocation', function() {
geolocation = new AMap.Geolocation({
enableHighAccuracy: true,//是否使用高精度定位,默認:true
timeout: 10000, //超過10秒后停止定位,默認:無窮大
buttonOffset: new AMap.Pixel(10, 20),//定位按鈕與設(shè)置的??课恢玫钠屏?,默認:Pixel(10, 20)
zoomToAccuracy: true, //定位成功后調(diào)整地圖視野范圍使定位位置及精度范圍視野內(nèi)可見,默認:false
buttonPosition:'RB'
});
map.addControl(geolocation);
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, 'complete', c);//返回定位信息
AMap.event.addListener(geolocation, 'error', c); //返回定位出錯信息
});
function c(data){
self.gpsData(data)
callback(data)
}
}
--END--