-
-
Save varmais/74586ec1854fe288d393 to your computer and use it in GitHub Desktop.
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); | |
}); |
Thanks bro
Thanks, this helps a lot!
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 :(((
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?
THANK YOU FOR POSTING THAT SOLUTION!
I'm glad I stumbled upon this decision!
Thank you!
After hours of trying to solve it for my self. This freaking saved my life. I been at it for hours. THANK YOU!!
Am I the only one who gets an error while trying to access position.coords
? It says Property 'coords' does not exist on type '{}'.
So elegant, thank you so much :)
Thank you! You save my time :}
After trying to figure this out for a couple of hours, realised I needed a promise.. this solution worked great! thank you 👍
Thank you
Thank you very much!
Thanks. realy useful! (For me )) )