簡體   English   中英

javascript處理地理位置時導致錯誤

[英]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));

更新了JSFiddle示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM