passing a variable into a JSON URL in preload()

I'd like to pull weather information based on the user's location using the p5.geolocation library and Open Weather Map. I'm able to access latitude and longitude outside of preload, but when I attempt to do so within preload, they both show "undefined". What am I missing here?

var locationData;

function preload() {
  locationData =  getCurrentPosition();

  var currentLat = locationData.latitude;
  var currentLon = locationData.longitude;

  var url = "//api.openweathermap.org/data/2.5/weather?lat=" + currentLat + "&lon=" + currentLon + "&appid=[myAPIkey]";

  weather = loadJSON(url);
}

Answers

Sign In or Register to comment.