I'm pulling through the lat & lng data from an API request as below but i'm tring to get the markef to update every time I change the counrty in the dropdown menu (selCountry).
$('#selCountry').on('change', function(){
$.ajax({
url: "php/restCountries.php",
type: 'POST',
dataType: 'json',
data: {
country: $('#selCountry').val()
},
success: function(result) {
console.log('restCountries', result);
if (result.status.name == "ok") {
currencyCode = result.currency.code;
iso2CountryCode = result.data.alpha2Code;
var countryName2 = result.data.name;
countryName = countryName2.replace(/\s+/g, '_');
var latlng = result.data.latlng
$('#countryName').html('<strong> Country: ' + result['data']['nativeName'] + '</strong>');
$('#txtCurrencyCode').html('<strong> Capital City: ' + result['data']['capital'] + '<br> Currency Symbol: '+ result.currency.symbol + '<br> Currency Name: ' + result.currency.name + '<br> Currency Code: ' + result.currency.code + '</strong>');
console.log(result.data.latlng);
I've tried changing the marker details to the latlng info which updates on selecting a new country but the marker does not update. What am I missing?
I have the marker outside the ajax call and if I move it inside the call, the whole map crashes out and does not load.
L.marker([latlng], {icon: redMarker}).bindPopup(result['data']['capital']).addTo(map);
Any advice would be greatly appriciated.
Thank You