1
我在寫一個位置服務,它調用新的html5 navigator.geolocation類。但是,除非將「maximumAge」值更改爲「99999」,否則該服務將始終返回這些屬性的默認值,但我擔心它只能在短時間內運行。Navigator.geolocation不更新打印稿屬性
這裏是我的locationService:
export class LocationService {
private static _instance: LocationService = null;
public Latitude: KnockoutObservable<number> = ko.observable(0);
public Longitude: KnockoutObservable<number> = ko.observable(2);
constructor() {
navigator.geolocation.getCurrentPosition((position) => {
this.Longitude(position.coords.longitude);
this.Latitude(position.coords.latitude);
},() => {
alert('please use HTML5 enabled browser');
// TODO: use yahoo API and postcode from DB to get location long & lat fall back for browsers not supporting this call
}, {
timeout: 10000,
maximumAge: 1,
enableHighAccuracy: true
}
);
}
public static GetInstance(): LocationService {
if (LocationService._instance === null) {
LocationService._instance = new LocationService();
}
return LocationService._instance;
}
private noLocation() {
}
}
這裏是我如何使用它在迪朗達爾部件:
import locationService = require("services/locationService");
export class testWidget{
public locationServ: locationService.LocationService = locationService.LocationService.GetInstance();
public Latitude: KnockoutObservable<number> = ko.observable(1);
public Longitude: KnockoutObservable<number> = ko.observable(3);
constructor() {
this.Latitude.subscribe(function (newLat) {
alert("test change : " + newLat);
});
this.Latitude(this.locationServ.Latitude());
this.Longitude(this.locationServ.Longitude());
}
public activate() {
alert("test: " + this.Latitude() + this.Longitude());
}
}
return new testWidget();
此代碼提示 '測試的變化:0',然後 '測試:02'除非我改變了超時時間,在這種情況下,它會提醒真正的長和長值。
任何和所有的幫助非常感謝,因爲我整天都把我的頭髮拉出來。提前致謝。
謝謝您的回答。這是真的,但我想從geolaction調用的結果:position.coords.longitude&position.coords.latitude在服務的構造函數中調用(在屬性的默認值之後) – 2014-09-30 08:25:39