[英]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.