Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radius Search PHP, MYSQL and Google Maps

Im searching a database with the latitude and longitude of a location. I want to retrieve all of the locations within a certain radius.

I then encode the returned results into a JSON and retrieve the data using ajax, however I get an undefined error meaning that there is no data returned from the database.

Can anybody see where im going wrong?

Heres my query

$sql="SELECT *, ACOS( SIN( RADIANS( `lat` ) ) * SIN( RADIANS( $fLat ) ) + COS( RADIANS( `lat` ) )
* COS( RADIANS( $fLat )) * COS( RADIANS( `lng` ) - RADIANS( $fLon )) ) * 6380 AS `distance`
FROM `markers`
WHERE ACOS( SIN( RADIANS( `lat` ) ) * SIN( RADIANS( $fLat ) ) + COS( RADIANS( `lat` ) )
    * COS( RADIANS( $fLat )) * COS( RADIANS( `lng` ) - RADIANS( $fLon )) ) * 6380 < 10
    ORDER BY `distance`";


    $result = mysql_query($sql);

    while($r = mysql_fetch_assoc($result)) $rows[] = $r;

    echo json_encode($rows); 
like image 878
lnelson92 Avatar asked May 17 '12 20:05

lnelson92


1 Answers

I'm not much help with trig or SQL, I've just seen enough Google maps questions to point you to this tutorial:

https://developers.google.com/maps/articles/phpsqlsearch#findnearsql

There is a query for finding spots by distance in a radius that you can try, hope it works for you.

Use 3959 (in miles) or 6371 (in km)

like image 187
Heitor Chang Avatar answered Sep 19 '22 12:09

Heitor Chang