0

I am working on an app that display user's position info (latitude,...) with ionic 3 to do that am using the geolocation plugin but I keep getting undefined variable here's my code.

posit(){
    this.geolocation.watchPosition({enableHighAccuracy:true}).subscribe((data)=>{
        this.latitude=data.coords.latitude;
        this.longitude=data.coords.longitude;
        this.altitude= data.coords.altitude;
        console.log("geolocalisation");
        this.prec=data.coords.accuracy;
        this.speed=data.coords.speed;
        this.timestamp=this.time_stamp(data.timestamp);
        this.testvar=[data.coords.latitude,data.coords.longitude];
        console.log(this.prec)//i get the true value 
    });
    console.log(this.prec)//i get undefined
    return this.testvar;
}

what am i doing wrong here?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • You're just trying to use it before it has a value; the callback is **asynchronous**, it happens later, after `posit` has returned. See the linked question's answers for details. – T.J. Crowder Apr 06 '18 at 10:59
  • I did i get 1120 – KARIM-OUSS Apr 06 '18 at 10:59
  • You are binding value to this.prec after promise resolve. That's why you are getting value inside subscribe. You need to return value from inside subscribe. Take a look at callback function / promise / async await – HADI Apr 06 '18 at 11:09

0 Answers0