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