import { Injectable } from '@angular/core';
import { Observer, Observable,BehaviorSubject } from 'rxjs';

/**
 * GeolocationService class.
 * https://developers.google.com/maps/documentation/javascript/
 * https://dev.w3.org/geo/api/spec-source.html
 */
@Injectable()
export class GeolocationService {

  private positionObserver$:BehaviorSubject<GeolocationPosition>  = new BehaviorSubject<GeolocationPosition>(null);

  constructor() {
    navigator.geolocation.watchPosition(
      (position: GeolocationPosition) => {
        this.positionObserver$.next(position);
      },
      (error: GeolocationPositionError) => {
        console.debug('Geolocation service: ' + error.message);
      },
      {
        enableHighAccuracy: true,
        timeout: 5000,
        maximumAge: 0
      }
    );
  }


  getCurrentPosition(): Observable<GeolocationPosition> {
    return this.positionObserver$;
  }
}
