我试图开发一个小型网站,它从设置的半径从数据库检索结果,并将它们放置在地图上。 我使用谷歌地图API来获取纬度和长度从那里搜索条件,将这些传递给我的PHP脚本,它查询数据库并返回结果集作为JSON对象。搜索数据库与经纬度和长半径使用php mysql
我有一个小的问题,使用json将经纬度和长度都发送到php。
我的主要问题是,我的SQL搜索数据库似乎是错误的,因为它只是检索我的数据库中的前10个结果。我希望它从搜索点返回半径范围内的前10个结果。
这里是我的Ajax代码
function showCarPark(location)
{
var lat = location.lat();
var lng = location.lng();
//alert("Lat: " +lat.toFixed(6));
//alert("Lng: " +lng.toFixed(6));
document.getElementById('carParkResults').innerHTML = "";
var criterion = document.getElementById("address").value;
var count = 0;
$.ajax({
url: 'process.php',
type: 'GET',
data: "lat=" + lat + "&lng=" + lng,
dataType: 'json',
success: function(data)
{
jQuery.each(data, function()
{
$('<p>').text("Car Park: " + data[count].name).appendTo('#carParkResults');
placeCarParks(data[count].postcode,data[count].name, data[count].street, data[count].type);
count++;
});
},
error: function(e)
{
//called when there is an error
console.log(e.message);
alert("error" + e.message);
}
});
这里是我的PHP脚本
$rad = 20;
$lat = $_GET['lat'];
$lng = 1.4681464; //put a temporary number in as it wont pass the lng in the JSON
$sql="SELECT *, (3959 * acos(cos(radians('".$lat."')) * cos(radians(lat)) * cos(radians(long) - radians('".$lng."')) + sin(radians('".$lat."')) *
sin(radians(lat))))
AS distance
FROM carpark HAVING distance < 15 ORDER BY distance LIMIT 0 , 10";
$result = mysql_query($sql);
while($r = mysql_fetch_assoc($result)) $rows[] = $r;
echo json_encode($rows);
在我的表中的列被称为lat和长 任何帮助表示赞赏。
它不适用于WHERE,因为您使用了您的计数变量。所以你需要使用HAVING,而不是工作。 玩得开心;) – EliaszKubala