Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a table called osm_places with place_name geometry, place_type and many other info related to the city. My database system is PostGIS.

My requirement is:

Given a latitude and longitude (which may or many not be the exact lat and long stored in the database), I need to find the nearest city or place_name from the table osm_places with the help of the input latitude and longitude. Any help is highly appreciated.

Is there a nearest neighbor implementation in php available for this or SQL to handle this?

share|improve this question
Need more information about your setup. Is it PostGIS or Microsoft or something else? – Taylor H. Feb 4 '13 at 15:01

There is a function in pgRouting called find_node_by_nearest_link_within_distance(), which does exactly what you need. See

share|improve this answer

Geonames API can also do this - you will need an account:

Live Demo

Example - but you need to use your own account (replace username=demo)


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.