Last active
June 12, 2023 11:37
-
-
Save Tazaf/a12764e80007613e68d6dcbf818c328a to your computer and use it in GitHub Desktop.
Geolocation module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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