0

I would like to make add a feature to my search field. So you can write Close to: Copenhagen (example).

It should then take like 5 km range around /inside Copenhagen (copenhagen is capital of denmark), and get the addresses that are within, and show them as results..

So it takes all addresses, checks if they are within 5 km range around copenhagen and then display those that are.

Is this possible to do? If not, any simliar ways to do this? What should i look into?

Karem
  • 17,615
  • 72
  • 178
  • 278

2 Answers2

1

You can use (for example) the Google Maps API to geocode a text description of a location (the search term, and the addresses) into a GPS coordinate location. You then check which of the locations in your database is within range of the search term location. The terms and conditions of the use of that API require you to use the geocoding results to display a Google Map though.

Determining "within range" efficiently might be tricky though, you generally want either a GIS-capable database to do the calculations for you. A hackish shortcut that's easy to implement over any DB is taking the points 5 kilometers (or whatever distance you want) north, east, south, and west of the search term location, and then looking for addresses that have latitudes and longitudes between the minimum and maximum latitudes and longitudes that gives you. This page lists what the required formulas are. (This approach will not work if the searched area is near where longitude "wraps around" and doesn't search a rectangle because of the curvature of the earth, but not many people search for restaurant reviews or what have you in the Arctic or the middle of the Pacific.)

millimoose
  • 39,073
  • 9
  • 82
  • 134
  • Ok so I would need to convert the search input field, e.g Copenhagen (i may need to write more specific maybe) to a gps coordinate. And then i take the addresses from the database -> make them to gps coordinate and check which of them is within? this is what i understood from this is this right? – Karem Oct 21 '11 at 21:51
  • @Karem: Yes, that's basically right. Ideally you want to convert the addresses you're storing into GPS coordinates early and store the coordinates in your database. – millimoose Oct 21 '11 at 22:52
  • Thanks for your explanation. Please see my first problem in making this http://stackoverflow.com/questions/7858905/how-to-take-5-km-points-in-all-directions-of-a-location-geocoding-google-api – Karem Oct 22 '11 at 10:36
  • @Karem: There's a link to the required formulae in my answer. – millimoose Oct 22 '11 at 12:06
  • can you please refer what in the page specifically i need maybe? – Karem Oct 22 '11 at 12:24
  • [Destination point given distance and bearing from start point](http://www.movable-type.co.uk/scripts/latlong.html#destPoint) – millimoose Oct 22 '11 at 12:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4460/discussion-between-karem-and-inerdia) – Karem Oct 22 '11 at 14:09
0

You should look at geocoding.

One way to do that is to use the Google Maps API, it allows you to convert an address into coordinates and from those you can easily calculate addresses that are within a certain distance.

Take a look at: http://code.google.com/apis/maps/documentation/webservices/index.html

I'm sure that if you look for "geocoding" you will find many other solutions, if you don't like this particular one.

Marcel Offermans
  • 3,313
  • 1
  • 15
  • 23