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

Geolocation

Newbie here, I'm trying to get the location of a device and use the coords to make other API calls.

I've been unable to use the coords outside of the locationSuccess function. Is there a way to get around this, has anyone succeeded with this?

Best Answer
0 Votes
1 REPLY 1

You can define a function which accepts lat/lon and call that function within locationSuccess, or you can define some variables outside the scope of locationSuccess and set them within locationSuccess.

 

function doSomethingWithLocation(lat, lon) {
  console.log(`Do something: ${lat} || ${lon}`)
}

function locationSuccess(position) {
  console.log("Latitude: " + position.coords.latitude,
              "Longitude: " + position.coords.longitude);
  doSomethingWithLocation(position.coords.latitude, position.coords.longitude)
}

// OR

var myLat, myLon;

function locationSuccess(position) {
  console.log("Latitude: " + position.coords.latitude,
              "Longitude: " + position.coords.longitude);
  myLat = position.coords.latitude;
  myLon = position.coords.longitude;
}
Best Answer
0 Votes