我有一张地图,上面有地方。地点的坐标在 MySQL 中保存为 lat/lon 字符串 ("3456,789")。我正在尝试编写一个函数,该函数将为我提供一个以我的坐标为中心点的正方形形状的所有坐标数组。然后,我将访问数据库以查看此数组中是否有任何其他地方有坐标。
我可以定义正方形的角点,并且可以编写一个笨重的函数来用 for 循环或类似的东西一个一个地用所有坐标填充数组,但我想知道是否有一个聪明的算法可以照顾这对我来说。下面的示例与预期的输出:
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");
}
有谁知道这样做的简单方法?
为了及早回答一些潜在的问题,坐标必须作为纬度/经度字符串存储在数据库中,目前没有办法解决这个问题。这与谷歌地图无关。
提前谢谢了
编辑 为方便起见添加了基本 SQL 查询:
"SELECT * FROM ".$database_table_prefix."user_objects_metadata WHERE..."
// the coordinates are saved in column "value"