Here is my code:
return this.userService.getPosition().pipe(
switchMap(() => {
return this.get('/places', { point: this.userService.coords });
}),
);
Sometimes, the position cannot be retrieved, for example if there is no https in Google Chrome or if the user does not allow it.
In such cases, I still need to execute
return this.get('/places', { point: this.userService.coords });
However, in this scenario, this.userService.coord
will have a value of null
.
This is how the service code looks like:
export class UserService {
constructor() {}
coords: Coordinates;
getPosition(): Observable<any> {
return new Observable(observer => {
if (window.navigator && window.navigator.geolocation) {
window.navigator.geolocation.getCurrentPosition(
position => {
this.coords = [position.coords.latitude, position.coords.longitude];
observer.next(this.coords);
observer.complete();
},
error => observer.error(error),
);
} else {
this.coords = null;
observer.error('Unsupported Browser');
}
});
}
}
Currently, if the outer observable returns an error, the inner observable is not executed.