Created
August 18, 2017 06:52
-
-
Save coquin/f1429e2861ecbf3d2cad50e2479b6b9c to your computer and use it in GitHub Desktop.
Getting current location of the user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Note: promise will be rejected if user won't allow browser | |
| // to detect location | |
| function getLocation () { | |
| return new Promise((resolve, reject) => { | |
| if (navigator.geolocation) { | |
| navigator.geolocation.getCurrentPosition(resolve, reject) | |
| } else { | |
| reject() | |
| } | |
| }) | |
| } | |
| // Usage | |
| getLocation() | |
| .then((pos) => pos.coords) | |
| .catch(() => { default_lat, default_lng }) | |
| .then((coords) => doStuff(coords)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment