Skip to content

Instantly share code, notes, and snippets.

@Tazaf
Last active June 12, 2023 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tazaf/a12764e80007613e68d6dcbf818c328a to your computer and use it in GitHub Desktop.
Save Tazaf/a12764e80007613e68d6dcbf818c328a to your computer and use it in GitHub Desktop.
Geolocation module
import { Observable } from 'rxjs';
const hasApi = 'geolocation' in navigator;
export const Geolocation = {
getCurrentPosition(
options: PositionOptions = {}
): Promise<GeolocationPosition> {
return new Promise((resolve, reject) => {
if (!hasApi) {
reject('The Geolocation API is not available on this browser');
}
navigator.geolocation.getCurrentPosition(resolve, reject, options);
});
},
watchPosition(
options: PositionOptions = {}
): Observable<GeolocationPosition> {
return new Observable((subscriber) => {
if (!hasApi) {
subscriber.error(
'The Geolocation API is not available on this browser'
);
subscriber.complete();
}
navigator.geolocation.watchPosition(
(position) => subscriber.next(position),
(error) => subscriber.error(error),
options
);
});
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment