openstreetmapoverpass-apinominatim

How to get the address from coordinates with Open Street Maps API?


I developed an open-source APP in Cordova (it uses Javascript) and I'm using the Google Maps API, though as the APP is becoming popular my bill is increasing (not nice for a free, ad-free APP). Thus I'd like to move to Open Street Maps.

I've been reading the docs about the Overpass API but I see no simple clear examples of code implementation. I know the sever to use, that I should use HTTP GET requests and use their special XML syntax. But it's not clear how do I pass that XML to the GET request. Furthermore the examples regarding coordinates provides as input a boundary box, not a point (or a point is regarded as a square whose corners are the same?).

<union>
  <bbox-query s="51.249" w="7.148" n="51.251" e="7.152"/>
  <recurse type="up"/>
</union>
<print mode="meta"/>

Could you kindly provide a simple example in Javascript (for example with $.ajax) on how to get the address of a certain location by providing the geo-coordinates to the API?


Solution

  • After some hours around, I share with you the working solution. Apparently, we should use the Nominatim service from Open Street Maps, and therein the reverse geocoding service. Please read the usage policy to avoid abuses, since this is a completely free service.

    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)
    })