我的本地服务器上有一个MySQL表。该表包括用户标记的地点的纬度和经度。我试图让所有在距离提供的纬度和经度1公里范围内标记他们位置的人。但我的结果不是预期的。
表:map_locations
id user_id lat lng place_name
1 1 28.584688 77.31593 Sec 2, Noida
2 2 28.596026 77.314494 Sec 7, Noida
3 5 28.579876 77.356131 Sec 35, Noida
4 1 28.516831 77.487405 Surajpur, Greater Noida
5 1 28.631451 77.216667 Connaught Place, New Delhi
6 2 19.098003 72.83407 Juhu Airport, Mumbai
这里是PHP脚本
$lat = '28.596026';
$long = '77.314494';
$query = "SELECT id,
(6371 * acos( cos( radians($lat) ) * cos( radians('lat') ) *
cos( radians('lng') - radians($long)) +
sin(radians($lat)) * sin(radians('lat')) )
) as distance
FROM map_locations
HAVING 'distance' < 1
ORDER BY id
LIMIT 25";
$_res = mysqli_query($conn, $query) or die('Error query: '.$query);
$detail = array();
$i=0;
while($row = $_res->fetch_assoc()) {
$detail[$i] = $row['id'];
$i++;
}
print_r($detail);
结果:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
查询返回表中的所有记录。谁能告诉我查询中的错误?
答案 0 :(得分:0)
是的,所有记录都可能不到1公里,我建议你尝试改变距离。
让我再次修改你的查询,然后检查它是否给你一个完美的结果
$query = "SELECT id,(6371 * acos( cos( radians($lat) ) * cos( radians('lat') ) * cos( radians('lng') - radians($long)) + sin(radians($lat)) * sin(radians('lat')) )) as distance FROM map_locations HAVING 'distance' < 0.5 ORDER BY id LIMIT 25";
此外,您可以通过更改手动拉取lng,
进一步检查$lat = '21.591026';
$long = '79.314994';
如果发生任何查询,请告诉我。