09-25-2018 16:23
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


09-25-2018 16:23
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hello,
this is a question I am shy to ask as it clearly demonstrate my ignorance in Javascript.
I was able to get GPS location and work out some API calls to a weather provider. However this was done putting all the API call functions nested into the GPS call function. I find this a little confusing so I wanted to have the lat,long coordinates made available out of the function, sort of
console.log("Companion code started");
import { geolocation } from "geolocation";
geolocation.getCurrentPosition(locationSuccess, locationError);
var lat;
var long;
function locationSuccess(position) {
lat = position.coords.latitude;
long = position.coords.longitude;}
function locationError(error) {
console.log("Error: " + error.code,
"Message: " + error.message);
}
console.log("Latitude: " + lat, "Longitude: " + long);
Obviously this doesn't work as lat, long variables are not made available out of the function. How do I achieve that?
Thanks
Answered! Go to the Best Answer.

Accepted Solutions
09-26-2018 12:37
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


09-26-2018 12:37
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
The problem with your example is that the lat/long only get populated once the gps getCurrentPosition() is successful. Your console.log() is happening immediately, it's not waiting for the callback.
This example is probably what you're after. https://steemit.com/programming/@leighhalliday/converting-geolocation-from-callbacks-into-async-awai...
09-26-2018 12:37
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


09-26-2018 12:37
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
The problem with your example is that the lat/long only get populated once the gps getCurrentPosition() is successful. Your console.log() is happening immediately, it's not waiting for the callback.
This example is probably what you're after. https://steemit.com/programming/@leighhalliday/converting-geolocation-from-callbacks-into-async-awai...
09-26-2018 20:48
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


09-26-2018 20:48
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Thanks a lot, I will give it a try!

