FarmMapsLib/projects/common-map/src/fm-map/services/geolocation.service.ts

40 lines
1.0 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Observer, Observable } from 'rxjs';
/**
* GeolocationService class.
* https://developers.google.com/maps/documentation/javascript/
* https://dev.w3.org/geo/api/spec-source.html
*/
@Injectable()
export class GeolocationService {
/**
* Tries HTML5 geolocation.
*
* Wraps the Geolocation API into an observable.
*
* @return An observable of Position
*/
getCurrentPosition(): Observable<Position> {
return Observable.create((observer: Observer<Position>) => {
// Invokes getCurrentPosition method of Geolocation API.
navigator.geolocation.watchPosition(
(position: Position) => {
observer.next(position);
},
(error: PositionError) => {
console.debug('Geolocation service: ' + error.message);
//observer.error(error);
},
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
}
);
});
}
}