Trying to return the latitude and longitude value from leaftlet geocoder

C

clestcruz

Guest
I've been trying to figure out how I can fetch or return the value from geocoder block and use the value in another function. I keep getting an variable is not defined error


geocoder = new L.Control.Geocoder.Nominatim();
var location = $('.location').text();

geocoder.geocode(location, function(results) {
var latLng = new L.LatLng(results[0].center.lat, results[0].center.lng);
var marker = new L.Marker (latLng);
// console.log(Object.values(latLng));

var geocodeLocation = Object.values(latLng);
});

function loc() {

console.log(geocodeLocation);

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og=="
crossorigin=""></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/perliedman-leaflet-control-geocoder/1.9.0/Control.Geocoder.js"></script>


<span class="location">Philippines</span>

Continue reading...
 
Top