[英]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. 因此,我正在使用Google Maps进行学校项目,我也想获得自己的位置。 However, when I run my script: http://pastebin.com/1kU5DSe0
但是,当我运行脚本时: http : //pastebin.com/1kU5DSe0
I get this error: Uncaught TypeError: Cannot set property 'lat' of undefined
(on line 15) 我收到此错误:
Uncaught TypeError: Cannot set property 'lat' of undefined
(第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
: 在
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);
};
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 编辑:我之前没注意到这一点,但因为你是在传递
this.updateLocation
作为回调的getCurrentPosition
的背景下, this
是不同的,这意味着this.pos.lat
会
navigator.geolocation.getCurrentPosition(this.updateLocation.bind(this));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.