我想使用Leaflet地图和函数,可以传递Lat/Lng并收到包含地址的文本消息。
我正在尝试使用Esri插件,但是我做错了什么。 目前我能够在函数内获取地址,但我不知道如何将其正确传递给变量。
以下是我的代码:
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);
这里有一个完整的例子: https://jsfiddle.net/5aq6z1vL/
如何像函数一样使用地理编码:
var address = convertToAddress([40.725, -73.985]);
function convertToAddress(]lat,lon])
{
// here return address after geocoding
}