Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

GPS Location function

ANSWERED

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

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

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...

 

 

View best answer in original post

Best Answer
2 REPLIES 2

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...

 

 

Best Answer

Thanks a lot, I will give it a try!

Best Answer
0 Votes