It sounds super simple, but I am looking to update my global variables ('latitude' and 'longitude') every time the map is clicked, as need to use these values elsewhere. Updating the variables from inside the function does not apply. Thus, the alerts show 'undefined'.
Any guidance would be much appreciated :-)
// set global vars
var latitude;
var longitude;
var popup = L.popup();
// create function to populate pop-up and global vars
function onMapClick(e) { // show pop-up when map is clicked
popup .setLatLng(e.latlng) .setContent("You clicked the map at " + e.latlng.toString()) .openOn(mymap); // populate pop-up with lat/long values
latitude = e.latlng.lat // populate global var to use elsewhere
longitude = e.latlng.lng // populate global var to use elsewhere
}
mymap.on('click', onMapClick); // call function when cliked on map
alert(latitude) // output is 'undefined'
alert(longitude) // output is 'undefined'