0

Hi I want to refine my "search by geolocation" for my platform( http://carrotleads.com) .

I am able to receive the geolocation of a user who lands on my site. Using his IP, I can get the below JSON http://www.freegeoip.net/json/ This is not always consistent as sometimes some fields don't get populated.

My database has customer records that references their area of operation.

How do I calculate the distance of the user to each of my customer and sort the search results in ascending order by distance ( closest customers/entities displayed first.)

For ex: http://farmersmarkets.org.au/markets

displays all markets in my state sorted alphabetically. How can the results be displayed, sorted by distance.

I code on the LAMP stack but happy to look at examples in other languages and adapt it.

kpax
  • 35
  • 2
  • 6
  • I've done this. I researched it. – Popnoodles May 26 '14 at 00:37
  • Can I request you to share your knowledge pls. Would appreciate the help. – kpax May 26 '14 at 00:42
  • Not without seeing some effort made. This isn't what S.O. is for. – Popnoodles May 26 '14 at 00:43
  • Thanks mate. I shall remember that. – kpax May 26 '14 at 00:49
  • You need to find the location of each point of interest, then determine how to calculate the distance—by road (fastest, shortest, best surface, etc.) great circle, rumb line, etc. Then you have something to sort. What have you tried? – RobG May 26 '14 at 00:57
  • I have lat and long for each user. Will need to find lat & long for each customer based on their location. I don't know how to calculate the distance yet. There must be some lib or math calc to find out distance using lat & long. Hence my question here. – kpax May 26 '14 at 01:02
  • I got the link. http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points Will post my version of answer here for other folks if thread is still up – kpax May 26 '14 at 01:08

1 Answers1

1

Pretty interesting question.
Did some searching and I found this cool article: http://aravindtrue.wordpress.com/2009/06/30/calculate-distance-using-latitude-and-longitude-php-mysql/

The author posts a function in PHP for finding the distance between 2 points using longitude and latitude. This is really convenient since the freegeoip api you've listed returns long/lat.

function getDistanceBetweenPointsNew($latitude1, $longitude1,
$latitude2, $longitude2, $unit = 'Mi')
{
   $theta = $longitude1 - $longitude2;
   $distance = (sin(deg2rad($latitude1)) *
   sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) *
   cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
   $distance = acos($distance);
   $distance = rad2deg($distance);
   $distance = $distance * 60 * 1.1515;
   switch($unit)
   {
      case 'Mi': break;
      case 'Km' : $distance = $distance *1.609344;
   }
   return (round($distance,2));
}

He also goes on to list an example SQL query that will return results within a distance

$qry = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) * 
sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * 
cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*
pi()/180))))*180/pi())*60*1.1515) as distance FROM `MyTable` 
WHERE distance <= ".$distance."

Definitely check out that article, I think it contains exactly all the information you're looking for.