0
votes

I have a table with lon/lat coordinates What I need is to get users from mysql table that are in some distance from given coordinates (it can be circle or square)

I use GreatCircle class (Great Circle Distance question) to get north east west soth coordinates in some distance from starting point:

$distance = 300;

$GreatCircle = new GreatCircle();

// North
$new_coordinates = $GreatCircle->getPositionByDistance($distance, 0, $lon, $lat);
$N_lon = $new_coordinates['lon'];
//$N_lat = $new_coordinates['lat'];

// East
$new_coordinates = $GreatCircle->getPositionByDistance($distance, 90, $lon, $lat);
//$E_lon = $new_coordinates['lon'];
$E_lat = $new_coordinates['lat'];

// South
$new_coordinates = $GreatCircle->getPositionByDistance($distance, 180, $lon, $lat);
$S_lon = $new_coordinates['lon'];
//$S_lat = $new_coordinates['lat'];

// West
$new_coordinates = $GreatCircle->getPositionByDistance($distance, 270, $lon, $lat);
//$W_lon = $new_coordinates['lon'];
$W_lat = $new_coordinates['lat'];

Now, can i create sql like this to get other coordinates from my table 300 meters around?

$GreatCircle = 
"l.longitude <= $E_lon 
AND l.latitude <= $N_lat
AND l.longitude >= $W_lon
AND l.latitude >= $S_lat";
1

1 Answers

2
votes

that will work if you dont mind a square. (although keep in mind that sometimes the lat/long values are negative...)

ususally, you would pass two sets of lat/long pairs to the function, and get back a distance as the result. then you would just check if the distance was within your limit. (that would be the circle version)