简体   繁体   中英

javascript causes an error when dealing with geo location

So I'm working on a school project by using google maps, and I want to get my position aswell. However, when I run my script: http://pastebin.com/1kU5DSe0

I get this error: Uncaught TypeError: Cannot set property 'lat' of undefined (on line 15)

Does anyone see what I'm doing wrong? :/

On the updateLocation function, you need to use this.pos.lat instead of GeoManager.pos.lat :

GeoManager.prototype.updateLocation = function (position) {
    this.pos.lat = position.coords.latitude;
    this.pos.lng = position.coords.longitude;

    console.log(this.pos);
};

EDIT: I didn't notice this before, but since you're passing in this.updateLocation as the call back of getCurrentPosition , the context of this is different, which means this.pos.lat will be

navigator.geolocation.getCurrentPosition(this.updateLocation.bind(this));

Updated JSFiddle Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM