2021年4月10日星期六

Variables becomes undefined outside of function

So I've been using leaflet.js to create a map of vaccination sites in my home state and was trying to use routing to find the closest site to the user, but the variable somehow becomes undefined when I log or try to call it outside the function. I'm relatively new to js and learned about leaflet when using folium in python. In python I think you would just declare you're changing a global variable or better yet use return but I'm not really sure how to do that with js and the leaflet library.

var self_lat;  var self_long;  ...  var mymap = L.map('mapid').setView([34.7465, -92.2896], 6);  ...  function onLocationFound(e) {      console.log(e);      self_lat =  e.latitude;      self_long = e.longitude;      L.marker([e.latitude, e.longitude], {icon: home_marker}).bindPopup("YOU ARE HERE!").addTo(mymap);      console.log(typeof(e.latitude))      console.log(typeof(e.longitude))  }    function onLocationError(e) {      alert(e.message);  }  mymap.locate({setView: false, maxZoom: 16});  mymap.on('locationfound', onLocationFound);  mymap.on('locationerror', onLocationError);    ...  L.Routing.control({  waypoints: [  L.latLng([self_lat, self_long]),  L.latLng([34.7465, -92.2896])  ]  }).addTo(mymap);  
https://stackoverflow.com/questions/67040691/variables-becomes-undefined-outside-of-function April 11, 2021 at 09:28AM

没有评论:

发表评论