I can't understand the execution order of the following. This part of a larger script, but the $( "#select-overlay" )
starts this going.
function findSelectedMap(mapID) {
$.getJSON('maps.json', function (json) {
json.forEach(function (entry) {
if (entry.maps.id == mapID) {
changeLayerTo = entry.maps.url;
maxZoom = entry.maps.zoom;
} // end if
}); // end json.forEach
}); // end $.getJSON
} // end findSelectedMap
function overlaySelector() {
$("#select-overlay").change(function () {
mapID = $("#select-overlay input[type='radio']:checked").val();
findSelectedMap(mapID); // using mapID, find the url, zoom for overlayMap selected
currentLayer = L.tileLayer(changeLayerTo).addTo(map);
// more map stuff
}); // end $( "#select-overlay" ).
} // end overlaySelector function
overlaySelector
is called when the page loads in a Rails app
<script>
$(document).on("turbolinks:load", function()
{
overlaySelector();
});
</script>
I think changeLayerTo
should be set on line 5 before currentLayer = L.tileLayer(changeLayerTo).addTo(map);
is executed. The findSelectedMap(mapID)
function is entered (I have a bunch of console.logs to track), and then execution jumps to the currentLayer = L.tileLayer(changeLayerTo).addTo(map);
line (and errors, but I manually defined changeLayerTo
just above so execution could continue, and then the json.forEach(function(entry)
line executes and iterates over the dozen entries in maps.json
and finds the correct value. But of course I need that value sooner.
What am I missing? Originally the findSelectedMap(mapID)
was embedded in the overlaySelector
function but I pulled it out to see if it would help, and it seemed like cleaner coding. All the variables are declared outside the functions. I don't imagine I'm approaching the problem in the best way, but I need the url and zoom (and eventually some other data related to the map).