Converted observer to behaviorsubject
This commit is contained in:
parent
51bb5b3833
commit
4a0474546c
@ -1,5 +1,5 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observer, Observable } from 'rxjs';
|
||||
import { Observer, Observable,BehaviorSubject } from 'rxjs';
|
||||
|
||||
/**
|
||||
* GeolocationService class.
|
||||
@ -9,23 +9,15 @@ import { Observer, Observable } from 'rxjs';
|
||||
@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.
|
||||
private positionObserver$:BehaviorSubject<Position> = new BehaviorSubject<Position>(null);
|
||||
|
||||
constructor() {
|
||||
navigator.geolocation.watchPosition(
|
||||
(position: Position) => {
|
||||
observer.next(position);
|
||||
this.positionObserver$.next(position);
|
||||
},
|
||||
(error: PositionError) => {
|
||||
console.debug('Geolocation service: ' + error.message);
|
||||
//observer.error(error);
|
||||
},
|
||||
{
|
||||
enableHighAccuracy: true,
|
||||
@ -33,7 +25,10 @@ export class GeolocationService {
|
||||
maximumAge: 0
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
getCurrentPosition(): Observable<Position> {
|
||||
return this.positionObserver$;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user