[英]javascript causes an error when dealing with geo location
因此,我正在使用Google Maps進行學校項目,我也想獲得自己的位置。 但是,當我運行腳本時: http : //pastebin.com/1kU5DSe0
我收到此錯誤: Uncaught TypeError: Cannot set property 'lat' of undefined
(第15行)
有人看到我在做什么錯嗎? :/
在updateLocation
函數上,您需要使用this.pos.lat
而不是GeoManager.pos.lat
:
GeoManager.prototype.updateLocation = function (position) {
this.pos.lat = position.coords.latitude;
this.pos.lng = position.coords.longitude;
console.log(this.pos);
};
編輯:我之前沒注意到這一點,但因為你是在傳遞this.updateLocation
作為回調的getCurrentPosition
的背景下, this
是不同的,這意味着this.pos.lat
會
navigator.geolocation.getCurrentPosition(this.updateLocation.bind(this));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.