我试图编写一个搜索位置附近的搜索栏。现在,它是硬编码的。该语句有效,直到我为经度和纬度添加like语句。有更好的方法吗?
$searchString = explode(' ', $_GET["searchString"]);
$useLocation = $_GET["useCurrentLocation"];
if($useLocation == 1){
foreach($searchString AS $key){
$stmt = $db->prepare("SELECT * FROM locations WHERE Name LIKE '%{$key}%' AND Longitude Like '-79.8579' And Latitude Like '43.5237'");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()){
echo $row['Name'];
}
}
}
答案 0 :(得分:0)
您可以在互联网上找到许多关于“如何按位置搜索”的解决方案
Stackoverflow上有一些答案:
或者在Google上:
这是我对此的最后一个问题:
我通过Google Maps API获得经度/纬度。输入地址, 并获取GPS坐标。 之后,用SQL计算位置和周围的对象
$ longitude_ref = 7.694731; $ latitude_ref = 48.560719;
SELECT *
FROM maps
WHERE (
6366 *
acos(
cos(radians(48.560719)) -- $latitude_ref
*
cos(radians(`latitude`))
*
cos(radians(`longitude`) - radians($longitude_ref)) -- $longitude_ref
+
sin(radians(48.560719)) * sin(radians(`latitude`)) -- $latitude_ref
)
) <= 2 -- r in Km (here: 2 km)
您还可以计算两点之间的距离:
SELECT pg.id, pg.*, pg1.*, 6366 * 2 *
atan2(
sqrt(
SIN((RADIANS(pg1.latitude) - RADIANS(pg.latitude)) / 2) * SIN((RADIANS(pg1.latitude) - RADIANS(pg.latitude)) / 2) + COS(RADIANS(pg.latitude)) * COS(RADIANS(pg1.latitude)) * SIN((RADIANS(pg1.longitude) - RADIANS(pg.longitude)) / 2) * SIN((RADIANS(pg1.longitude) - RADIANS(pg.longitude)) / 2)
),
sqrt(
1 - SIN((RADIANS(pg1.latitude) - RADIANS(pg.latitude)) / 2) * SIN((RADIANS(pg1.latitude) - RADIANS(pg.latitude)) / 2) + COS(RADIANS(pg.latitude)) * COS(RADIANS(pg1.latitude)) * SIN((RADIANS(pg1.longitude) - RADIANS(pg.longitude)) / 2) * SIN((RADIANS(pg1.longitude) - RADIANS(pg.longitude)) / 2)
)
) as calculated_distance_final_km
FROM perso_gps pg
WHERE pg.calculated_dist IS NULL
互联网上的许多例子:)