0

I'm doing a website which every user put his address and the website will match every one with the closest person, I embedded Google map API for the direction but how can the website match users ?

if you could give me examples or any strategy that can help me to reach the goal?

waely
  • 525
  • 1
  • 4
  • 7
  • 1
    Save longitude and latitude of all addresses captured (use google maps api). Then you should be able to form some sort of radius equation to match the closest address. That's what I would do – Samuel Cook Apr 03 '13 at 23:27
  • can you tell me how I can get the longitude and latitude ? – waely Apr 04 '13 at 00:08

2 Answers2

1

You can learn how to acquire the lat/long at: Get latitude and longitude automatically using php, API

Store the lat/long of each user within their data within your DB.

Additionally, it would be wise to store the COUNTRY of each user - so your query will be reduced. Highly advise to also store regional data such as ZIP/POSTAL to further reduce your query load.

Then on whatever php page you are using, you will already have the regional data (as explained above) of the user you are searching on. With that data loop through all of the other members within the same ZIP/POSTAL region and query their stored lat/long.

Once you have the lat/long of others within the same ZIP/POSTAL region, you can use a function such as: http://snipplr.com/view/2531/

or, you can perform a lot of this via a SQL statement as explained https://stackoverflow.com/a/7672821/797620 but I have never tested that statement myself.

This page at google provides another and very well detailed method that you could give a try: https://developers.google.com/maps/articles/phpsqlsearch_v3

Community
  • 1
  • 1
Abela
  • 1,225
  • 3
  • 19
  • 42
  • You're over concerned about reducing the query-load by adding country and zipcode as restraints. What is a person lives next to the border and across the borders lives the nearest person? – stUrb Apr 04 '13 at 04:11
  • Agreed in concept. In most situations, for most social websites these days, people tend to only care about their own country/region, so that was the focus on my answer. However, yes, going that route does limit the list of available lat/long in the manner you pointed out. – Abela Apr 04 '13 at 04:14
0

This question screams for a PostgreSQL/POSTGIS solution. Store the lat-lon in a table with some meta info if necessary. And then let the magic of POSTGIS do its thing.

PostgreSQL/Postgis is optimized to make these geo-spatial related queries! Actually in the new version > 9.1 the K-Nearest Neighbour operator is introduced, which returns the N-nearest neighbours (hence the name):

SELECT * FROM your_table ORDER BY your_table.geom <-> "your location..." LIMIT 10

So that's easy :)

"The <-> is a “distance” operator, but it only makes use of the index when it appears in the ORDER BY clause. Between putting the operator in the ORDER BY and using a LIMIT to truncate the result set, we can very very quickly (less than 10ms on a 2M record table, in this case) get the 10 nearest points to our test point."

Read more here.

stUrb
  • 6,612
  • 8
  • 43
  • 71