# How to wait for Geolocation results?

**URL:** <https://forum.ionicframework.com/t/how-to-wait-for-geolocation-results/90338>\
**Category:** ionic-v3\
**Created:** [May 15, 2017, 12:08pm UTC](https://forum.ionicframework.com/t/how-to-wait-for-geolocation-results/90338 "2017-05-15T12:08:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![flightboy](https://avatars.discourse-cdn.com/v4/letter/f/bb73d2/32.png) [@flightboy](https://forum.ionicframework.com/u/flightboy)\
**Post date:** [May 15, 2017, 12:08pm UTC](https://forum.ionicframework.com/t/how-to-wait-for-geolocation-results/90338/1 "2017-05-15T12:08:38Z")

</div>

Hi, all,  
Thanks in advance.

I’m using Geolocation API to make application to acquire latitude and longitude.  
I want to acquire the latitude and longitude at the initial startup and store it in the global variable.  
So I wrote the following code, but ’ionViewDidEnter’ will be executed before success is called.

`Test.ts`

> import { Component } from ‘@angular/core’;  
> import { NavController, NavParams } from ‘ionic-angular’;  
> import { Setting } from ‘…/…/app/variable’  
> import ‘rxjs/add/operator/map’;

> @Component({  
> selector: ‘page-test’,  
> templateUrl: ‘test.html’  
> })

> export class TestPage {  
> constructor(public navCtrl: NavController, public navParams: NavParams) {  
> navigator.geolocation.getCurrentPosition(function(position) {  
> **Setting.Latitude = position.coords.latitude;**  
> **Setting.Longitude = position.coords.longitude;**  
> });  
> }

> ```
> ionViewWillLoad(){
> console.log(Setting.Latitude + ' ' + Setting.Longitude );
> 
> ```
> 
> // Latitde and Longitude are null.  
> }

> ```
> ionViewDidLoad() {
> console.log(Setting.Latitude + ' ' + Setting.Longitude );
> 
> ```
> 
> // Latitde and Longitude are null.  
> }

> ```
> ionViewDidEnter(){
> console.log(Setting.Latitude + ' ' + Setting.Longitude );
> 
> ```
> 
> **// Latitde and Longitude are null. I wanna do something here;**  
> }  
> }

`Variable.ts`

> export class Settings {  
> public Longitude: number;  
> public Latitude: number;  
> constructor() {  
> this.Longitude = null;  
> this.Latitude = null;  
> }  
> }  
> export var Setting: Settings;  
> Setting = new Settings();

I want to get the latitude and longitude in the constructor and then call the first page display “ionViewDidEnter”, but I do not know how.  
Please tell me how to wait for results.  
Thanks.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [May 15, 2017, 8:53pm UTC](https://forum.ionicframework.com/t/how-to-wait-for-geolocation-results/90338/2 "2017-05-15T20:53:19Z")

</div>

Use [the ionic-native wrapper](https://ionicframework.com/docs/native/geolocation/) that provides Promises.

---

<div class="post-metadata">

**Author:** ![flightboy](https://avatars.discourse-cdn.com/v4/letter/f/bb73d2/32.png) [@flightboy](https://forum.ionicframework.com/u/flightboy)\
**Post date:** [May 16, 2017, 12:28am UTC](https://forum.ionicframework.com/t/how-to-wait-for-geolocation-results/90338/3 "2017-05-16T00:28:43Z")

</div>

Thank you for your reply.  
It solved in this way!
