UNPKG

@ng-web-apis/geolocation

Version:

This is a library for declarative use of Geolocation API with Angular

58 lines (51 loc) 2.55 kB
import * as i0 from '@angular/core'; import { InjectionToken, inject, Injectable } from '@angular/core'; import { Observable, finalize, shareReplay } from 'rxjs'; import { WA_NAVIGATOR } from '@ng-web-apis/common'; const WA_GEOLOCATION = new InjectionToken('[WA_GEOLOCATION]', { factory: () => inject(WA_NAVIGATOR).geolocation, }); /** * @deprecated: drop in v5.0, use {@link WA_GEOLOCATION} */ const GEOLOCATION = WA_GEOLOCATION; const WA_POSITION_OPTIONS = new InjectionToken('[WA_POSITION_OPTIONS]', { factory: () => ({}) }); /** * @deprecated: drop in v5.0, use {@link WA_POSITION_OPTIONS} */ const POSITION_OPTIONS = WA_POSITION_OPTIONS; const WA_GEOLOCATION_SUPPORT = new InjectionToken('[WA_GEOLOCATION_SUPPORT]', { factory: () => !!inject(GEOLOCATION), }); /** * @deprecated: drop in v5.0, use {@link WA_GEOLOCATION_SUPPORT} */ const GEOLOCATION_SUPPORT = WA_GEOLOCATION_SUPPORT; class GeolocationService extends Observable { constructor() { const geolocationRef = inject(GEOLOCATION); const geolocationSupported = inject(GEOLOCATION_SUPPORT); const positionOptions = inject(POSITION_OPTIONS); let watchPositionId = 0; super((subscriber) => { if (!geolocationSupported) { subscriber.error('Geolocation is not supported in your browser'); } watchPositionId = geolocationRef.watchPosition((position) => subscriber.next(position), (positionError) => subscriber.error(positionError), positionOptions); }); return this.pipe(finalize(() => geolocationRef.clearWatch(watchPositionId)), shareReplay({ bufferSize: 1, refCount: true })); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: GeolocationService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: GeolocationService, providedIn: 'root' }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: GeolocationService, decorators: [{ type: Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: function () { return []; } }); /** * Generated bundle index. Do not edit. */ export { GEOLOCATION, GEOLOCATION_SUPPORT, GeolocationService, POSITION_OPTIONS, WA_GEOLOCATION, WA_GEOLOCATION_SUPPORT, WA_POSITION_OPTIONS }; //# sourceMappingURL=ng-web-apis-geolocation.mjs.map