I have a map with places on it. The coordinates for the places are saved in MySQL as a lat/lon string ("3456,789"). I'm trying to write a function that will give me an array of all coordinates in the shape of a square with my coordinates as the centre-point. I will then hit the database to see if any other places have coordinates in this array.
I can define the corner points of the square and can write a clunky function to fill the array with all the coordinates one by one with a for loop or something like that, but I'm wondering if there is a clever algorithm that can take care of this for me. Example below with expected output:
function getNearbyPlaces($distance = 4){
$radius = $distance / 2;
$coords = explode(",",$object->metadata["Coordinates"]["value"]); // array("3456","789")
$topLeft = array($coords[0] - $radius,$coords[1] - $radius);
$topRight = array($coords[0] + $radius,$coords[1] - $radius);
$botLeft = array($coords[0] - $radius,$coords[1] + $radius);
$topRight = array($coords[0] + $radius,$coords[1] + $radius);
// calculate all coords within square here
// Expected output
// array("3454,787","3455,787","3456,787","3457,787","3458,787","3454,788","3455,788","3456,788","3457,788","3458,788","3454,789","3455,789","3456,789","3457,789","3458,789","3454,790","3455,790","3456,790","3457,790","3458,790","3454,791","3455,791","3456,791","3457,791","3458,791");
}
Does anyone know of a simple way of doing this?
To answer some potential questions early, the coordinates must be stored as lat/lon strings in the database, there is currently no way around this. This has no connection with Google maps.
Many thanks in advance
EDIT Added base SQL query for convenience:
"SELECT * FROM ".$database_table_prefix."user_objects_metadata WHERE..."
// the coordinates are saved in column "value"