我有一张场地表,其中有纬度,经度列,表示它的位置。我想从场地表中获取位置,并获得靠近用户当前位置的位置。
当前位置将作为函数的参数给出。
对于第一次,我试图从场地表中获得所有纬度和经度,并创建一个数组。
然后我尝试计算场地阵列中每个场地的距离。但它为我创建的$ distance数组给出了一个错误。
我是php的新手,所以不知道怎么做。
我想返回距用户当前位置10公里附近的位置。
搜索功能:
public function searchVendors($fields)
{
try {
$lat1 = $fields -> lat1;
$lat2 = $fields -> lon1;
$con = DB::getConnection();
$query = "SELECT `lattitude`,`longitude` FROM venues";
$rs = mysqli_query($con,$query) or die (json_encode(array("result"=>-1, "message"=>mysql_error())));
$n = mysqli_num_rows($rs);
$venues = array();
if ( $n > 0 ) {
while ( $row = mysqli_fetch_assoc($rs)) {
$venues[] = $row;
}
$distances = array();
foreach($venues as $venue) {
$distance = (((acos(sin((".$latitude."*pi()/180)) * sin((`Latitude`*pi()/180))+
cos((".$latitude."*pi()/180)) * cos((`Latitude`*pi()/180)) *
cos(((".$longitude."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515*1.609344)
$distances[] = $distance;
}
$result = array("result"=>1, "message"=>"success", "distances" => $distances);
return json_encode($result);
} else {
$result = array("result"=>-1, "message"=>"Event list is empty");
return json_encode($result);
}
} catch(DBConnectionException $e) {
$result = array("result"=>-1, "message"=> $e -> getMessage());
return json_encode($result);
}
return null;
}
getVendors php:
<?php
header("Content-type: application/json");
if ( $_SERVER['REQUEST_METHOD']=='POST') {
include_once ("../include/Vendor.php");
try {
$con = DB::getConnection();
$raw = file_get_contents("php://input");
$data = json_decode($raw, true);
$v = new Vendor();
$response = $v -> searchVendors($data);
json_encode($response);
if ( $response == null ) {
$response = json_encode(array("result" => -2, "message" => "Empty result"));
echo $response;
} else {
echo $response;
}
} catch(Exception $e) {
$result = array("result" => -1, "message" => $e -> getMessage());
echo json_encode($result);
}
}
?>
编辑:搜索功能
public function searchVendors($fields)
{
try{
$lat1 = $fields -> lat1;
$lat2 = $fields -> lon1;
$con = DB::getConnection();
$query = "SELECT `lattitude`,`longitude` FROM venues";
$rs = mysqli_query($con,$query) or die (json_encode(array("result"=>-1, "message"=>mysql_error())));
$n = mysqli_num_rows($rs);
$venues = array();
if ( $n > 0 ) {
while ( $row = mysqli_fetch_assoc($rs)) {
$venues[] = $row;
}
$distances = array();
foreach($venues as $venue)
{
$distance = (((acos(sin((".$latitude."*pi()/180)) * sin((`Latitude`*pi()/180))+
cos((".$latitude."*pi()/180)) * cos((`Latitude`*pi()/180)) *
cos(((".$longitude."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515*1.609344);
if($distance < 10)
{
$distances[] = $distance;
}
}
$result = array("result"=>1, "message"=>"success", "distances" => $distances);
return json_encode($result);
} else {
$result = array("result"=>-1, "message"=>"Event list is empty");
return json_encode($result);
}
} catch(DBConnectionException $e) {
$result = array("result"=>-1, "message"=> $e -> getMessage());
return json_encode($result);
}
return null;
}
得到这样的输出:
{
"result": 1,
"message": "success",
"distances": [
0,
0,
0,
0,
0,
0,
0,
0
]
}
我怎样才能做到这一点?我的代码应该改变什么?有人可以帮忙吗?谢谢..
答案 0 :(得分:0)
您可以在MySQL语句本身中计算距离和顺序。您需要在搜索时根据用户提供的当前地址计算Lat / Lon,然后使用以下代码:
SELECT *,
( 3959 * acos( cos( radians($latitude) ) * cos( radians( lattitude ) )
* cos( radians(longitude) - radians($longitude)) + sin(radians($latitude))
* sin( radians(lattitude)))) AS distance
FROM venues
ORDER BY distance;