1

Actually i have millions of records of people in my server database and i want to fetch data of those people who are 5 miles near to me(i mean a circle with 5 miles radius )...

i am sending this data to an iPhone through webservice...

Please tell me how to do this on my PHP server....

Kumar sonu
  • 535
  • 11
  • 24
  • 1
    this is a bit complicated to answer without additional information. what have you tried till now and where is your problem with this? how does the data in your database look like? do you want to know how to built this webservice or just how to write the sql-query? – oezi Mar 01 '11 at 09:46
  • @oezi-----my database has person name, address, zip,name ...etc and i want to get their full detail on the basis of gps coordinates sent by iPhone...... – Kumar sonu Mar 01 '11 at 09:57
  • If you don't have lat/long for your persons table, then you'll need to convert zip to lat/long before you can calculate any distances – Mark Baker Mar 01 '11 at 10:07
  • @Mark Baker -- i have converted that already.... – Kumar sonu Mar 01 '11 at 10:14
  • in that case, read my answer; and look at the link I've included to see how to do this. – Mark Baker Mar 01 '11 at 10:15

2 Answers2

7

Probably this can help you

Let for example you have input latitude and longitude 37 and -122 in degree. And you want to search for users within 25 miles from current given latitude and longitude.

SELECT item1, item2, 
    ( 3959 * acos( cos( radians(37) ) 
                   * cos( radians( lat ) ) 
                   * cos( radians( lng ) 
                       - radians(-122) ) 
                   + sin( radians(37) ) 
                   * sin( radians( lat ) ) 
                 )
   ) AS distance 
FROM geocodeTable 
HAVING distance < 25 
ORDER BY distance LIMIT 0 , 20;

If you want search distance in kms. Then replace 3959 with 6371 in above query.

Get nearest places on Google Maps, using MySQL spatial data

Community
  • 1
  • 1
Gaurav
  • 28,447
  • 8
  • 50
  • 80
2

With millions of people records in your server database (I hope you're adhering to data protection laws), then you'll need to reduce the workload of calculating all these distances; otherwise you need to do the calculation for every single one of those millions of records.

Use PHP to calculate a "bounding box", which can be used to select a subset of likely candidates from your millions of people records, then only calculate the distance for that subset of people records. See this article from the movable type website for example PHP code describing how to do this, and an excellent description of how the method actually works.

Mark Baker
  • 209,507
  • 32
  • 346
  • 385