How to store geolocation in variable? Ionic3


#1

Hi guys, I’m trying to pass the geolocation coordinates from my Ionic 3 App to my Rails 5 API but I don’t know how I can’t get data this way:

I tried to create variables this way:
public lat: number;
public lng: number;

this.geo.getCurrentPosition().then((resp) => {

          alert(resp.coords.latitude); //here I can see the coordinate in alert
          alert(resp.coords.longitude); //here I also receive the coordinate in alert
          this.lat = resp.coords.latitude; //my variable receive the data here
          this.lng = resp.coords.longitude; //my variable receive the data here

}).catch((error) => {
console.log(‘Ocorreu um erro’, error);
});

BUT HERE I CAN`T USE THE COORDINATES TO BE SENT TO API:

let dados_app = JSON.stringify({
latitude: this…lat, //nothing here
longitude: this.lng, //nothing here
});

Do you know if have some special variable to get this data from this plugin?
Thanks for advance guys.


#2

It’s hard to say with what you’ve shown, but I’d imagine you’re not waiting for the promise to complete before sending the coordinates off.


#3

thanks for the answer SigmundFroyd!

in my alert messages I can see the altitude: -65.234242424 and the longitude: -23.45446445455 I just like to get this values and sent to my API to store in my database, but in my variable don’t arrive nothing:

here specifically:

let dados_app = JSON.stringify({
latitude: this.lat,
longitude: this.lng,
});

Do you know if it’s possible to be done?


#4

What SigmundFroyd mean is that your let dados_app = JSON.stringify… codeblock is executed befor your .then codebolock executes and you alert shows up.

Put this JSON.stringify thing and your API Call all together in the .then block where your alert is…


#5

Hi Jack! I’m looking here to my code to trying to do what you said, but look what I have here:
I don’t know how to do that in my case… can you help?

baixar_parcela(data:any) {
let confirm = this.alertCtrl.create({
title: ‘Atenção!’,
message: ‘Este processo é irreversível, Você tem certeza de que está correta esta baixa?’,
buttons: [
{
text: ‘Cancelar’,
handler: () => {
console.log(‘Cancelou a baixa’);
}
},
{
text: ‘Confirmar’,
handler: () => {

         //pegando a geolocalização
         this.geo.getCurrentPosition().then((resp) => {
          //alert(resp.coords.latitude);
          //alert(resp.coords.longitude);
          this.lat = resp.coords.latitude;
          this.lat = resp.coords.longitude;
         }).catch((error) => {
           console.log('Ocorreu um erro', error);
         });

       //loading
         let loading = this.loadingCtrl.create({
         spinner: 'hide',
         content: 'Carregando aguarde…'
       });

       this.nativeStorage.getItem('current_session').then
           ((dados_sessao) =>
           {

            let dados_app = JSON.stringify({
             cardToken: 'G0d1$@Bl3T0d0W4Th3V3Rth1Ng',
             receipt_id: data.receipt_id,
             id_consultor: dados_sessao.id_consultor,
             latitude: this.lat,
             longitude: this.lng,
             });

       const headers = new Headers({ 'Content-Type': 'application/json' });
       const options = new RequestOptions({ headers });

       return new Promise((resolve, reject) => {
         this.http
         .post(this.enderecoApi + '/baixa_parcela_emprestimo',{ dados_app: dados_app}, options)
         .toPromise()
         .then((result) => {


}


#6

Thanks Jacktoolsnet and SigmundFroyd!
fixed guys!

now I understood what you mean, solved God bless you guys!


#7

If you mind you can marked the question as solved.