如何使用 Open Street Maps API 从坐标获取地址?

Joã*_*ira 3 openstreetmap overpass-api nominatim

我在 Cordova 中开发了一个开源应用程序(它使用 Javascript),并且我正在使用 Google Maps API,尽管随着该应用程序变得流行,我的账单正在增加(对于免费、无广告的应用程序来说这不是很好)。因此我想转向开放街道地图。

我一直在阅读有关 Overpass API 的文档,但没有看到简单清晰的代码实现示例。我知道要使用的服务器,我应该使用 HTTP GET 请求并使用其特殊的 XML 语法。但不清楚如何将该 XML 传递给 GET 请求。此外,关于坐标的示例提供了边界框作为输入,而不是点(或者点被视为角相同的正方形?)。

<union>
  <bbox-query s="51.249" w="7.148" n="51.251" e="7.152"/>
  <recurse type="up"/>
</union>
<print mode="meta"/>
Run Code Online (Sandbox Code Playgroud)

您能否提供一个简单的 Javascript 示例(例如使用$.ajax)来说明如何通过向 API 提供地理坐标来获取特定位置的地址?

Joã*_*ira 7

几个小时后,我与您分享工作解决方案。显然,我们应该使用Open Street Maps 的Nominatim服务,以及其中的反向地理编码服务。请阅读使用政策以避免滥用,因为这是一项完全免费的服务。

const coord = [38.748666, -9.103002] 
fetch(`https://nominatim.openstreetmap.org/reverse?lat=${coord[0]}&lon=${coord[1]}&format=json`, {
  headers: {
    'User-Agent': 'ID of your APP/service/website/etc. v0.1'
  }
}).then(res => res.json())
  .then(res => {
    console.log(res.display_name)
    console.log(res.address)
})
Run Code Online (Sandbox Code Playgroud)


NVR*_*VRM 5

另一个端点和一个简短的片段:

fetch("https://nominatim.openstreetmap.org/search.php?q=48.886,2.343&polygon_geojson=1&format=json")
    .then(response => response.json())
    .then(j => {
      console.log(j[0].display_name)
})
Run Code Online (Sandbox Code Playgroud)