mapping - php algorithm that collects all coordinates into array from within 2D square -


i have map places on it. coordinates places saved in mysql lat/lon string ("3456,789"). i'm trying write function give me array of coordinates in shape of square coordinates centre-point. hit database see if other places have coordinates in array.

i can define corner points of square , can write clunky function fill array coordinates 1 one loop or that, i'm wondering if there clever algorithm can take care of me. example below 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 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 know of simple way of doing this?

to answer potential questions early, coordinates must stored lat/lon strings in database, there no way around this. has no connection google maps.

many in advance

edit added base sql query convenience:

"select * ".$database_table_prefix."user_objects_metadata where..." // coordinates saved in column "value" 

it seems need select database points within range.

without table data impossible write full sql query you, had fiddle , produced following should convert strings floats can compare. should work if strings same format, otherwise can same using instr() select location of comma.

select cast( concat_ws('.', left( '1234,567', 4 ), right( '1234,567', 3 ) ) binary )  

with might try calculating expected min , max lat , long in php plug them in vis:

select lat,long locations cast( concat_ws('.', left( lat, 4 ), right( lat, 3 ) ) binary ) >= '$minlat' , cast( concat_ws('.', left( lat, 4 ), right( lat, 3 ) ) binary ) <= '$maxlat' , cast( concat_ws('.', left( long, 4 ), right( long, 3 ) ) binary ) >= '$minlong' , cast( concat_ws('.', left( long, 4 ), right( long, 3 ) ) binary ) <= '$maxlong' 

i realise isn't quite had in mind in question me seems way go.

edit:

coping variable precision of lat , long done instr() calling:

 cast( concat_ws('.', left( lat, instr(lat,',')-1),substring( lat, instr(lat,',')+1 ) ) binary ) 

you need use substring() method rather right() though.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

c++ - End of file on pipe magic during open -