I offer a service that monitors the position of devices:
getLocation(opts): Observable<any> {
return Observable.create(observer => {
if (window.navigator && window.navigator.geolocation) {
window.navigator.geolocation.watchPosition((position) => {
observer.next(position);
}, (error) => {
switch (error.code) {
case 1:
observer.error('errors.location.permissionDenied');
break;
case 2:
observer.error('errors.location.positionUnavailable');
break;
case 3:
observer.error('errors.location.timeout');
break;
}
}, opts);
} else {
observer.error('errors.location.unsupportedBrowser');
}
});
}
then retrieve latitude and longitude in the component:
ngOnInit() {
var source = this.locationService.getLocation({enableHighAccuracy:true, maximumAge:30000, timeout:27000});
source.subscribe(pos => {
this.lat = pos.coords.latitude;
this.long = pos.coords.longitude;
}, err => {
this.err = err;
console.log(err);
});
}
This code works well on browsers on MacBook and iPhone, allowing for position updates when the device moves.
However, on my iPad (which is Wi-Fi only and lacks GPS), it retrieves the position initially but after a few seconds, the service returns error code 2 indicating position unavailability, and the browser stops updating the position. I'm unsure if it has stopped working or continues to run but constantly returns error code 2.
My queries are:
- Does watchPosition require GPS to function? My MacBook also lacks GPS.
- If an error occurs and is returned from the observable, do I need to resubscribe to obtain data (position) again, or should I simply wait until the error resolves itself?