I’m trying to update the global url variable from within the success method inside the geoFindMe function. I can update url inside of the geoFindMe function (as long as it’s not within a method), but if I try to do so within a method it breaks. Help!
var url = "Hello World";
geoFindMe();
alert(url);
/*** FUNCTIONS ***/
function geoFindMe() {
var output = document.getElementById("out");
if (!navigator.geolocation){
output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
return;
}
function success(position) {
var latitude = Math.round(position.coords.latitude);
var longitude = Math.round(position.coords.longitude).toString();
var lat = latitude.toString();
var lon = longitude.toString();
output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>';
url = "https://fcc-weather-api.glitch.me/api/current?lat=" + lat + "&lon=" + lon;
alert(url);
}
function error() {
output.innerHTML = "Unable to retrieve your location";
}
output.innerHTML = "<p>Locating…</p>";
navigator.geolocation.getCurrentPosition(success, error);
}