我有一个功能如下。
我试图用
var testVar = new genericYiiLocation();
console.log(testVar.getLongitude());
但是,我 this.getLongitude() 总是有一个空的 this.longitude。
我检查了 this.longitude 是否包含在 locateSuccess(loc) 中设置时所期望的值,并且看起来没问题。
任何指导将不胜感激。
function genericYiiLocation() {
console.log('genericYiiLocation: Creating location handler');
this.longitude= '';
this.latitude= '';
this.accuracy= '';
if (Modernizr.geolocation) {
console.log('genericYiiLocation: Location supported');
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(locateSuccess, locateFail);
}
else {
alert('genericYiiLocation: Geolocation is not supported in your current browser.');
return false;
}
} else {
alert ('genericYiiLocation: no native location support');
return false;
}
function locateSuccess(loc){
console.log('genericYiiLocation: storing location data');
this.longitude = loc.coords.longitude;
}
// Unsuccessful geolocation
function locateFail(geoPositionError) {
switch (geoPositionError.code) {
case 0: // UNKNOWN_ERROR
alert('An unknown error occurred, sorry');
break;
case 1: // PERMISSION_DENIED
alert('Permission to use Geolocation was denied');
break;
case 2: // POSITION_UNAVAILABLE
alert('Couldn\'t find you...');
break;
case 3: // TIMEOUT
alert('The Geolocation request took too long and timed out');
break;
default:
}
}
this.getLongitude = function(){
console.log('long: '+this.longitude);
return this.longitude;
}
}