Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Geolocation to Promise wrap example
var getPosition = function (options) {
return new Promise(function (resolve, reject) {
navigator.geolocation.getCurrentPosition(resolve, reject, options);
});
}
getPosition()
.then((position) => {
console.log(position);
})
.catch((err) => {
console.error(err.message);
});
@kholiavko-roman

This comment has been minimized.

Copy link

kholiavko-roman commented Jun 19, 2017

Tnaks, it helps me!

@ye-s

This comment has been minimized.

Copy link

ye-s commented Jun 20, 2017

Thanks, I have been tinkering similar solution!

@HerbertLim

This comment has been minimized.

Copy link

HerbertLim commented Oct 1, 2017

Thank you so much. It helped me a lot.

@OlegPomazkov

This comment has been minimized.

Copy link

OlegPomazkov commented Dec 1, 2017

Thanks. realy useful! (For me )) )

@jonathanCNITA

This comment has been minimized.

Copy link

jonathanCNITA commented Mar 9, 2018

Thanks bro

@stuartg23

This comment has been minimized.

Copy link

stuartg23 commented Mar 20, 2018

Thanks, this helps a lot!

@erickvieira

This comment has been minimized.

Copy link

erickvieira commented Apr 30, 2018

One question: is this a solution to promise catch empty error in ionic geolocation.getCurrentPosition? I am looking for this in all web, since most than one week, so please tell me that is it :(((

@trandaison

This comment has been minimized.

Copy link

trandaison commented May 3, 2018

To check if Geolocation is supported.

getCurrentPosition: function () {
    if (navigator.geolocation) {
      return new Promise(
        (resolve, reject) => navigator.geolocation.getCurrentPosition(resolve, reject)
      )
    } else {
      return new Promise(
        resolve => resolve({})
      )
    }
  }
getCurrentPosition()
  .then(
    position => console.log(positon);
    if (position.coords) {
      position => console.log(positon);
    } else {
      alert('Geolocation is not supported by this browser.');
    }
  ).catch(
    // error => console.log(error);
    // Or
    error => {
      var msg = null;
      switch(error.code) {
        case error.PERMISSION_DENIED:
            msg = "User denied the request for Geolocation.";
            break;
        case error.POSITION_UNAVAILABLE:
            msg = "Location information is unavailable.";
            break;
        case error.TIMEOUT:
            msg = "The request to get user location timed out.";
            break;
        case error.UNKNOWN_ERROR:
            msg = "An unknown error occurred.";
            break;
      }
      alert(msg);
    }
  )

@erickvieira Did you try to catch via error code like above?

@davidhbigelow

This comment has been minimized.

Copy link

davidhbigelow commented Jan 27, 2019

THANK YOU FOR POSTING THAT SOLUTION!

@krobik

This comment has been minimized.

Copy link

krobik commented Mar 5, 2019

I'm glad I stumbled upon this decision!

@EdTimmer

This comment has been minimized.

Copy link

EdTimmer commented Apr 19, 2019

Thank you!

@alvarezaaronAI

This comment has been minimized.

Copy link

alvarezaaronAI commented May 19, 2019

After hours of trying to solve it for my self. This freaking saved my life. I been at it for hours. THANK YOU!!

@Jcharloi

This comment has been minimized.

Copy link

Jcharloi commented May 28, 2019

Am I the only one who gets an error while trying to access position.coords ? It says Property 'coords' does not exist on type '{}'.

@charmingelle

This comment has been minimized.

Copy link

charmingelle commented Sep 3, 2019

So elegant, thank you so much :)

@yuki0417

This comment has been minimized.

Copy link

yuki0417 commented Sep 7, 2019

Thank you! You save my time :}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.