09-25-2018 16:23
Fitbit Product Experts Alumni are retired members of the Fitbit Product Expert Program. Learn more
09-25-2018 16:23
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.
Best Answer09-26-2018 12:37
Fitbit Developers oversee the SDK and API forums. We're here to answer questions about Fitbit developer tools, assist with projects, and make sure your voice is heard by the development team.
09-26-2018 12:37
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
Fitbit Developers oversee the SDK and API forums. We're here to answer questions about Fitbit developer tools, assist with projects, and make sure your voice is heard by the development team.
09-26-2018 12:37
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
Fitbit Product Experts Alumni are retired members of the Fitbit Product Expert Program. Learn more
09-26-2018 20:48
Thanks a lot, I will give it a try!
Best Answer