我正在编写一个调用新的html5 navigator.geolocation类的位置服务。但是,除非我更改了' maximumAge'否则服务始终会返回这些属性的默认值。价值为' 99999'但后来我担心它只会在很短的时间内起作用。
这是我的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() {
}
}
这是我如何在durandal小部件中使用它:
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();
此代码提醒&#39;测试更改:0&#39;然后&#39;测试:02&#39;除非我改变超时,否则它会提醒实际的长和纬度值。
我非常感谢所有人的帮助,因为我整天都把头发拉了过来。提前谢谢。
答案 0 :(得分:0)
会员被称为
public Latitude: KnockoutObservable<number> = ko.observable(1);
public Longitude: KnockoutObservable<number> = ko.observable(3);
在构造函数体之前:
this.Latitude(this.locationServ.Latitude());
this.Longitude(this.locationServ.Longitude());
所以你有效地拥有:
public Latitude: KnockoutObservable<number> = ko.observable(1);
public Longitude: KnockoutObservable<number> = ko.observable(3);
this.Latitude(this.locationServ.Latitude());
this.Longitude(this.locationServ.Longitude());
因为locationServ有
public Latitude: KnockoutObservable<number> = ko.observable(0);
public Longitude: KnockoutObservable<number> = ko.observable(2);
0
,2
你得到0.2