A
A
Alexander Lee2018-01-03 13:04:38
Angular
Alexander Lee, 2018-01-03 13:04:38

Angular 4 - how to store received data for future use?

There is a service that receives the coordinates of the user's location. I call this service in another service, pull out the coordinates and save in global variables. I then use these variables to form the URL for the request to the server. But in the console I get an error as these global variables are undefined.
Tell me if it's not difficult what I'm doing wrong. Thanks in advance.

// service for get coords

@Injectable()
export class GoogleMapsSettingsService {

  constructor() {}

  getUserLocation(): Observable<any> {
    return Observable.create (observer => {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(position => {
          observer.next(position);
          observer.complete();
          console.log(position);
        }, error1 => {
          this.getUserDefaultLocation();
        });
      }
    });
  }

// second service for http request
@Injectable()
export class ConnectionService {
  baseUrl = 'https://SITE.com/';
  private lang$: string;
  private step$ = 1;
  private latitude$: number;
  private longitude$: number;
  private radius$ = 5;

constructor (private http: HttpClient, private systemTranslateService: SystemTranslateService, private googleMapsService: GoogleMapsSettingsService) 
{
this.googleMapsService.getUserLocation().subscribe(data => {
      this.latitude$ = data.coords.latitude;
      this.longitude$ = data.coords.longitude;
    });
  }
}


// получаю undefined вместо this.longitude$ // и this.latitude$
getDepartmentByLocation(): Observable<any> {
    return this.http.get(this.baseUrl + 'step/' + this.lang$ + '/' + this.step$ + '/area/' + this.latitude$ + '/' + this.longitude$ + '/' + this.radius$);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question