SIMPLE reverse geocoding using Nominatim
Asked Answered
D

2

5

i am developing an online mapping application using OpenLayers + OpenStreetMaps.

i need help implementing a simple reverse geocoding function in javascript (or php) that receives Latitude and Longitude and returns an Address.

i would like to work with Nominatim, if possible. i do NOT want to use Google, Bing or CloudMade or other proprietary solutions.

this link returns a reasonable response and i used simple_html_dom.php to break down the result but it is sort of an ugly solution.

<?php

include('simple_html_dom.php');

$url = "http://nominatim.openstreetmap.org/reverse?format=xml&lat=-23.56320001&lon=-46.66140002&zoom=27&addressdetails=1";
$html = file_get_html($url);
foreach ($html->find('road') as $element ) {
    echo $element;
}

?>  

any suggestions of a more elegant solution?

NOTE: as of Oct 2015, the request must include a valid email so as to "provide sufficient identification of your application".

Without an email, the return code will NOT be in xml or json format, but rather as error 509 "Bandwidth Exceeded", try using an html_dom library for scraping.

$url = "http://nominatim.openstreetmap.org/[email protected]&format=xml&lat=-23.56320001&lon=-46.66140002&zoom=27&addressdetails=1";
Dodie answered 6/6, 2012 at 23:32 Comment(1)
My first "true" gold badge. To me, 10K+ views reflects an undeniable relevance. Tks all.Dodie
J
7

You can request nominatim in JSON format, and pass a callback name, so that the response will be: callback(json).

Look at the doc : http://wiki.openstreetmap.org/wiki/Nominatim

And here’s a minimal example of use: http://jsfiddle.net/GWL7A/14/

Jacquiline answered 7/6, 2012 at 10:37 Comment(0)
D
1

I have been running tonio's solution for 4 years now. Though it implements well and i recommend it as such (even having approved as correct answer), but it has become clear that the answer is incomplete.

Depending on how your queries are implemented, you may run into issues with servers that may even lead to refusal of service.

Please be advised that, if you are using community Nominatim servers as proposed in his answer, it is best practice to:

  1. Cache recurrent geopoints with your determined margin of error and/or
  2. Run your own instance of Nominatim (publicly available, but LARGE DOWNLOAD)
Dodie answered 6/7, 2016 at 15:17 Comment(1)
Yes, running your own instance is a good idea... but it is HUGE. :-)Spermic

© 2022 - 2024 — McMap. All rights reserved.