您正在寻找的是“反向地理编码”。这里显示了一个很好的例子:https://gist.github.com/ThomasG77/26e61508217ba86a04c19a67cbda0e99 https://gist.github.com/ThomasG77/26e61508217ba86a04c19a67cbda0e99
在你的情况下,这大致可以翻译为:
function reverseGeocode(coords) {
fetch('http://nominatim.openstreetmap.org/reverse?format=json&lon=' + coords[0] + '&lat=' + coords[1])
.then(function(response) {
return response.json();
}).then(function(json) {
console.log(json);
});
}
map.on('click', function (evt) {
var coord = ol.proj.toLonLat(evt.coordinate);
reverseGeocode(coord);
});
响应将类似于以下内容:
{
"place_id":"109922016",
"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright",
"osm_type":"way",
"osm_id":"174849788",
"lat":"40.0498172",
"lon":"26.2183522194416",
"display_name":"Çanakkale Martyrs' Memorial, D550, Seddülbahir, Eceabat, Çanakkale, Marmararegion, Türkei",
"address":{
"memorial":"Çanakkale Martyrs' Memorial",
"road":"D550",
"village":"Seddülbahir",
"county":"Eceabat",
"state":"Marmararegion",
"country":"Türkei",
"country_code":"tr"
},
"boundingbox":[
"40.0496644",
"40.0499658",
"26.2181747",
"26.2185707"
]
}