我正在使用Phonegap的移动应用程序,它将使用地理位置。之前我使用了Geolocation,但是这次我认为为它创建一个新的对象包装会更好,因为大部分功能都是基于这个功能的,并且不希望以混乱的代码结束。Javascript Objects/Geolocation API
解决方案可能是直截了当的,但它击败了我,因为我从来没有在JS中使用对象做过任何非常先进的事情。下面是目前的代码(删除不需要的部分):
function Geolocation(maximumAge, accurate) {
this.settings = {
'maximumAge': maximumAge,
'accurate': accurate
}
}
Geolocation.prototype = {
position: {
latitude: null,
longitude: null,
altitude: null,
accuracy: null,
altitudeAccuracy: null,
heading: null,
speed: null
},
lastCheck: null,
watchId: null,
onSuccess: function(position) {
this.position.latitude = position.coords.latitude;
this.position.longitude = position.coords.longitude;
this.position.altitude = position.coords.altitude;
this.position.accuracy = position.coords.accuracy;
this.position.altitudeAccuracy = position.coords.altitudeAccuracy;
this.position.heading = position.coords.heading;
this.position.speed = position.coords.speed;
this.lastCheck = new Date(position.timestamp);
},
onError: function(error) {
console.log(error.code+' '+error.message);
},
getCoordinates: function() {
if (this.watchId == null) {
this.watchId = navigator.geolocation.watchPosition(this.onSuccess, this.onError, { maximumAge: this.settings.maximumAge, enableHighAccuracy: this.settings.accurate});
}
return {
latitude: this.position.latitude,
longitude: this.position.longitude
};
}
};
正如你也许已经注意到,当我打电话getCoordinates(),回调成功的功能是(我猜)出对象的范围,因此不知道“这个”是什么......任何想法如何解决这个问题或者什么是正确的实现?
谢谢你的时间!
后来编辑:我知道我需要修改该函数,只有在找到位置后才返回坐标。目前还不知道如何做到这一点,但如果您有任何提示,那将非常棒!