import { Component , OnInit , OnDestroy } from '@angular/core' ; import { Observable } from 'rxjs' ; import { interval } from 'rxjs' ; import { BackendService } from '../services/backend.service' ; @ Component ( { selector : 'app-dashboard' , templateUrl : './dashboard.component.html' , styleUrls : [ './dashboard.component.css' ] } ) export class DashboardComponent implements OnInit , OnDestroy { lat : number ; lng : number ; data ; stopCondition = false ; private querySubscription ; constructor ( private _backendService ) { } ngOnInit ( ) { const secondsCounter = interval ( 2000 ) ; this . querySubscription = secondsCounter . subscribe ( n => this . getLocations ( ) ) ; } getLocations ( ) { var positionOption = { enableHighAccuracy : false , maximumAge : Infinity , timeout : 60000 } ; var gpsSunccuss = function ( currentPosition ) { } ; var gpsFailed = function ( ) { } ; if ( navigator . geolocation ) { navigator . geolocation . getCurrentPosition ( ( position => { this . lat = position . coords . latitude ; this . lng = position . coords . longitude ; this . data = { "lat" : position . coords . latitude , "long" : position . coords . longitude } ; this . _backendService . setLocations ( this . data ) . subscribe ( ( res ) => console . log ( "Success" ) ) ; } ) , gpsFailed , positionOption ) ; } } ngOnDestroy ( ) { if ( this . querySubscription ) { this . querySubscription . unsubscribe ( ) ; } } }