Ionic call Function inside callback

Asked

Viewed 38 times

0

I’m trying to call a function inside the other function but it keeps giving:

ERROR Referenceerror: "closeGPS is not defined" enableGPS tab1.page.ts:35

my code currently:

enableGPS() {
    console.log("enable gps");
    this.gps = true;
    navigator.geolocation.getCurrentPosition(function (local) {
        sessionStorage.setItem('lat', local.coords.latitude);
        sessionStorage.setItem('lon', local.coords.longitude);
        closeGPS();
    });
}

closeGPS() {
    this.close = true;
}

1 answer

0


Switch the callback to a Arrow Function (() => {}), thus, the this will reference the location where closeGPS() is and then call her with the this

navigator.geolocation.getCurrentPosition((local) => {
    sessionStorage.setItem('lat', local.coords.latitude);
    sessionStorage.setItem('lon', local.coords.longitude);
    this.closeGPS();
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.