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);
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With