# Getting current location in ionic 3 problem

**URL:** <https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744>\
**Category:** ionic-v3\
**Created:** [September 18, 2017, 5:12am UTC](https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744 "2017-09-18T05:12:00Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![zuest](https://avatars.discourse-cdn.com/v4/letter/z/b9bd4f/32.png) [@zuest](https://forum.ionicframework.com/u/zuest)\
**Post date:** [September 18, 2017, 5:12am UTC](https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744/1 "2017-09-18T05:12:00Z")

</div>

im trying to use this function to get my current location:

```auto
getlocation(){
    let options = {
      timeout:10000,
      enableHighAccuracy:true
    };
    this.geolocation.getCurrentPosition(options).then((resp) => {
      console.log("inside func:",resp)
      return resp;
    });

```

inside the fuction i get my correct location howeve  
and in ngOnInit when i call this function…

```auto
  ngOnInit(){
    console.log(this.getlocation())
}

```

my output is

`undefined`

can somebody tell me why?

---

<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:** [September 18, 2017, 5:38am UTC](https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744/2 "2017-09-18T05:38:36Z")

</div>

> [@zuest](#):
>
> can somebody tell me why?

Because `getlocation()` doesn’t return anything. If you always declare return types for every function, you would have been told about this at compile time.

---

<div class="post-metadata">

**Author:** ![zuest](https://avatars.discourse-cdn.com/v4/letter/z/b9bd4f/32.png) [@zuest](https://forum.ionicframework.com/u/zuest)\
**Post date:** [September 18, 2017, 5:58am UTC](https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744/3 "2017-09-18T05:58:59Z")

</div>

Even so, i saved the value in a variable and returned and still get undefined, how can i fix this?

```auto

 getlocation(){
    let val;
    let options = {
      timeout:10000,
      enableHighAccuracy:true
    };
    this.geolocation.getCurrentPosition(options).then((resp) => {
      console.log("inside func:",resp)
      val = resp;
    });
return val;
  }

```

---

<div class="post-metadata">

**Author:** ![zuest](https://avatars.discourse-cdn.com/v4/letter/z/b9bd4f/32.png) [@zuest](https://forum.ionicframework.com/u/zuest)\
**Post date:** [September 18, 2017, 6:12am UTC](https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744/4 "2017-09-18T06:12:46Z")

</div>

i solved it by doing this:

```auto
  getlocation(){
    let val;
    let options = {
      timeout:10000,
      enableHighAccuracy:true
    };
    val = this.geolocation.getCurrentPosition(options).then((resp) => {
      console.log("inside func:",resp)
      return resp;
    });
  return val;
  }

```

```auto
 ngOnInit(){
   this.getlocation().then(val => {
      console.log(val)
    })

```

---

<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:** [September 18, 2017, 6:39am UTC](https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744/5 "2017-09-18T06:39:13Z")

</div>

Declare return types for all your functions, as I said before. The very first word of `getlocation()` should be `return`. As it is written, it is almost unreadable.

---

<div class="post-metadata">

**Author:** ![Judgewest2000](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/judgewest2000/32/113339_2.png) [@Judgewest2000](https://forum.ionicframework.com/u/Judgewest2000)\
**Post date:** [September 18, 2017, 7:11am UTC](https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744/6 "2017-09-18T07:11:59Z")

</div>

Sorry I also need to interject. @rapropos is absolutely correct.

Just return the promise as a whole, your code is unnecessarily complicated.

---

<div class="post-metadata">

**Author:** ![zuest](https://avatars.discourse-cdn.com/v4/letter/z/b9bd4f/32.png) [@zuest](https://forum.ionicframework.com/u/zuest)\
**Post date:** [September 18, 2017, 9:23am UTC](https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744/7 "2017-09-18T09:23:02Z")

</div>

thanks but im still facing issues im trying to set longitude and latitude variables to the lat and long and i get from the function…

```auto
public long:Number;
public lat:Number;
  constructor(private geolocation: Geolocation,public loadingCtrl: LoadingController) {
  }

```

```auto
 getlocation(){

    let options = {
      timeout:10000,
      enableHighAccuracy:true
    };
     return this.geolocation.getCurrentPosition(options);
  }

```

```auto
  ngOnInit(){
    this.getlocation().then(resp => {
      this.long = resp.coords.longitude;
      this.lat = resp.coords.latitude;``
    });
    console.log(this.long);

```

when i console log i get it undefinded,

---

<div class="post-metadata">

**Author:** ![Judgewest2000](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/judgewest2000/32/113339_2.png) [@Judgewest2000](https://forum.ionicframework.com/u/Judgewest2000)\
**Post date:** [September 18, 2017, 9:28am UTC](https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744/8 "2017-09-18T09:28:48Z")

</div>

MAN!!!

This will return nothing and rightly so…

```auto
console.log(this.long);

```

Coz it’ll be ran before your promise has completed.  
Think of it like a callback.

For testing try this…

```auto
 ngOnInit(){
    this.getlocation().then(resp => {
      //just to prove it's working at all
      alert(resp.coords.longitude);
    });
    //FORGET THIS console.log(this.long);

```

If you want your screen to react to the presence of the latitude and longitude, make them observable, or just run a function after they’ve been populated…

```auto
ngOnInit(){
      this.getlocation().then(resp => {
      this.long = resp.coords.longitude;
      this.lat = resp.coords.latitude;
      this.someFunction(); //this.long and this.lat are now populated
    });

```

---

<div class="post-metadata">

**Author:** ![zuest](https://avatars.discourse-cdn.com/v4/letter/z/b9bd4f/32.png) [@zuest](https://forum.ionicframework.com/u/zuest)\
**Post date:** [September 18, 2017, 10:55am UTC](https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744/9 "2017-09-18T10:55:16Z")

</div>

thanks, i got it, took a small reading on this

> **[JavaScript Promises: an Introduction  |  Web Fundamentals
       |  Google...](https://developers.google.com/web/fundamentals/primers/promises)**
>
> "Promises simplify deferred and asynchronous computations. A promise represents an operation that hasn't completed yet."

now i came to a conclusion if i want a specific value to be returned as a promise i need to wrap in a promise like this…

```auto
  getlongitude(){

    let options = {
      timeout:10000,
      enableHighAccuracy:true
    };

    return new Promise((resolve =>{
      this.geolocation.getCurrentPosition(options).then(resp => {
        resolve(resp.coords.longitude);
      });
    } ))
  }

```

so i would consume it where ever i need, but just my last question, is there a better way or different way of writing this?

---

<div class="post-metadata">

**Author:** ![Judgewest2000](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/judgewest2000/32/113339_2.png) [@Judgewest2000](https://forum.ionicframework.com/u/Judgewest2000)\
**Post date:** [September 18, 2017, 11:39am UTC](https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744/10 "2017-09-18T11:39:49Z")

</div>

If you’ve ever used a callback you’re 90% of the way with how promises work. Swim with the tide, not against it.

For any calling function you can write it like this…

```auto
async somethingToCallCoords(){
   var result = await getlongitude();
   console.log(result);
}

```

If you’re constantly wanting the latitude / longitude but don’t want to kill the mobile’s battery I’d suggest passing the values to a variable

```auto
 coords = {lat: 0, long: 0};

 ngOnInit(){
    this.getlocation().then(resp => {
      //just to prove it's working at all
      alert(resp.coords.longitude);
     this.coords= {lat: resp.coords.latitude, long: resp.coords.longitude};
    });

 someotherfunctionOffAButtonPress(){
  console.log(JSON.stringify(this.corrds));
 }

```

The first suggestion is probably easier for you to get on with.

---

<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:** [September 18, 2017, 8:52pm UTC](https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744/11 "2017-09-18T20:52:49Z")

</div>

> [@zuest](#):
>
> is there a better way or different way of writing this?

Absolutely.

```auto
getLongitude(): Promise<Number> {
  let options = {...};
  return this.geolocation.getCurrentPosition(options).then(rsp => rsp.coords.longitude);
}

```

You should never be typing “new Promise”.

---

<div class="post-metadata">

**Author:** ![zuest](https://avatars.discourse-cdn.com/v4/letter/z/b9bd4f/32.png) [@zuest](https://forum.ionicframework.com/u/zuest)\
**Post date:** [September 19, 2017, 6:05am UTC](https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744/12 "2017-09-19T06:05:55Z")

</div>

thanks so much guys \<3

---

<div class="post-metadata">

**Author:** ![NurGuz](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nurguz/32/111671_2.png) [@NurGuz](https://forum.ionicframework.com/u/NurGuz)\
**Post date:** [October 31, 2018, 3:51pm UTC](https://forum.ionicframework.com/t/getting-current-location-in-ionic-3-problem/105744/13 "2018-10-31T15:51:04Z")

</div>

Hi guys, I have this but getCurrentPositiondont do nothing:

```auto
  private getlocation() : Promise<Geoposition>{
        let val;
        let options = {
        timeout:10000,
        enableHighAccuracy:true
        };

        return this.geolocation.getCurrentPosition(options);
    }

 private loadMap(mapOptions: any) {
        this.getlocation().then( (position) => {
            this.latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
          });
}

```

Any idea?
