-1

I can't set value inside function. When I try to alert this value, I'm getting: undefined. How to take this value outside?

var startTrasyLat;
            
L.esri.Geocoding.geocode()
                .text(document.getElementById('start').value)
                .run(function (err, results, response) {
  if (err) {
    console.log(err);
    return;
  }
                  
  startTrasyLat = results['results'][0].latlng.lat;              
});
alert(startTrasyLat);
Neikar
  • 55
  • 6

1 Answers1

0

Looks like you are trying to call an asynchronous function. You should put the alert inside said function, like this:

var startTrasyLat;

L.esri.Geocoding.geocode().text(document.getElementById('start').value).run(function (err, results, response) {
  if (err) {
    console.log(err);
    return;
  }

  startTrasyLat = results['results'][0].latlng.lat;
  alert(startTrasyLat);
});