What I want is to use Leaflet maps + function where I can pass Lat/Lng and receive a text message with address.
I am trying to use esri plugin, however I am doing something wrong. At the moment I am abke to get the address inseide of function but I do not know how to properly pass it to variable.
Here is my code:
var map = L.map('map').setView([40.725, -73.985], 7);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var geocodeService = L.esri.Geocoding.geocodeService();
map.on('click', function(e) {
geocodeService.reverse().latlng(e.latlng).run(function(error, result) {
L.marker(result.latlng).addTo(map).bindPopup(result.address.Match_addr).openPopup();
});
});
var message;
message = geocodeService.reverse().latlng([40.725, -73.985]).run(function(error, result) {
//alert(result.address.Match_addr); //this alert works here ok and can retur addrress
return result.address.Match_addr;
});
//this alert won't work, why I can get the address here outside the function
alert(message);
and here is full example: https://jsfiddle.net/5aq6z1vL/
How to use geocoder as a function like:
var address = convertToAddress([40.725, -73.985]);
function convertToAddress(]lat,lon])
{
// here return address after geocoding
}