From aa257fec7d9eec81e49d34d17124e9c15601dca1 Mon Sep 17 00:00:00 2001 From: Ratnadeep Rajendra Kharade <92khra1mst@hft-stuttgart.de> Date: Sun, 8 Dec 2019 19:09:36 +0100 Subject: [PATCH] Created a common service for user location --- src/app/services/location.service.spec.ts | 12 +++++ src/app/services/location.service.ts | 54 +++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/app/services/location.service.spec.ts create mode 100644 src/app/services/location.service.ts diff --git a/src/app/services/location.service.spec.ts b/src/app/services/location.service.spec.ts new file mode 100644 index 0000000..6edba5d --- /dev/null +++ b/src/app/services/location.service.spec.ts @@ -0,0 +1,12 @@ +import { TestBed } from '@angular/core/testing'; + +import { LocationService } from './location.service'; + +describe('LocationService', () => { + beforeEach(() => TestBed.configureTestingModule({})); + + it('should be created', () => { + const service: LocationService = TestBed.get(LocationService); + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/location.service.ts b/src/app/services/location.service.ts new file mode 100644 index 0000000..befbc94 --- /dev/null +++ b/src/app/services/location.service.ts @@ -0,0 +1,54 @@ +import { Injectable } from '@angular/core'; +import { Geolocation } from '@ionic-native/geolocation/ngx'; +import { Subject } from 'rxjs'; +@Injectable({ + providedIn: 'root' +}) +export class LocationService { + public preiousUserPosition = { lat: 48.783480, lng: 9.180319 }; + public currentUserPosition = { lat: 48.783480, lng: 9.180319 }; + + liveLocationSubject = new Subject<any>(); //Decalring new RxJs Subject + + constructor(private geolocation: Geolocation) { + let watch = this.geolocation.watchPosition({ enableHighAccuracy: true, maximumAge: 10000 }); + watch.subscribe((position) => { + console.log('IN WATCHER') + console.log('lat'+ position.coords.latitude); + console.log('lng'+ position.coords.longitude); + this.currentUserPosition.lat = position.coords.latitude; + this.currentUserPosition.lng = position.coords.longitude; + this.preiousUserPosition.lat = position.coords.latitude; + this.preiousUserPosition.lng = position.coords.longitude; + this.getUserLiveLocation(this.currentUserPosition); + }, (errorObj) => { + console.log('error getting live location, setting to previous location'); + this.getUserLiveLocation(this.preiousUserPosition); + }); + } + + getUserLocation(): Promise<any> { + return new Promise((resolve, reject) => { + + this.geolocation.getCurrentPosition().then((resp) => { + let lat = resp.coords.latitude; + let lng = resp.coords.longitude; + this.currentUserPosition.lat = resp.coords.latitude; + this.currentUserPosition.lng = resp.coords.longitude; + this.preiousUserPosition.lat = resp.coords.latitude; + this.preiousUserPosition.lng = resp.coords.longitude; + resolve(this.currentUserPosition); + }, er => { + console.log('error getting location setting to previous location'); + resolve(this.preiousUserPosition); + }).catch((error) => { + console.log('error getting location setting to previous location'); + resolve(this.preiousUserPosition); + }); + }); + } + + getUserLiveLocation(location) { + this.liveLocationSubject.next(location); + } +} -- GitLab