I need to get the result from the variable latlng from CountryAPI() an pass it to myMap() as the code below, but I keep getting Uncaught ReferenceError: latlng is not defined:
const selectCountry = document.getElementById('country');
var latlng;
function countryAPI() {
const countrySelected = selectCountry.options[selectCountry.selectedIndex].text;
fetch(`https://restcountries.eu/rest/v2/name/${countrySelected}`)
.then(res => res.json()) //convert to JSON
.then(data => { //access the data from the JSON
latlng = data[0].latlng;
document.querySelector('.results').innerHTML = `<ul>
<li><img src=${data[0].flag} width="150px" align="center" alt="${data[0].name}"</li>
<li><b>Capital:</b> ${data[0].capital}</li>
<li><b>Population:</b> ${data[0].population}</li>
</ul>`
});
}
countryAPI();
selectCountry.addEventListener('change', countryAPI);
function myMap() {
var mapProp= {
center:new google.maps.LatLng(latlng),
zoom:5,
};
var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
myMap()?.then()callback instead, after the json is fetched and the variable is initialised.