場所を示す緯度、経度の列を持つ会場のテーブルがあります。会場テーブルから場所を取得し、ユーザーの現在の場所に近い場所を取得したいと考えています。
現在の場所は、関数へのパラメーターとして与えられます。
この 1 番目に、配列を作成した会場テーブルからすべての緯度と経度を取得しようとしました。
次に、会場配列の各会場の距離を計算しようとしました。しかし、距離配列は0,0のようです。$lat1、$lon1、$lat2、$lon2 の値は null です。
私はphpが初めてなので、これを行う方法がわかりません。
ユーザーの現在地から 10 km 近くの場所を返したい。
検索機能 :
public function searchVendors($lat1,$lon1)
{
// $lat2 = -10.4212157;
// $lon2 = 28.6031842;
// $lat1 = 19.9950050;
// $lon1 = 73.8413080;
try{
// $lat1 = $fields -> lat1;
// $lon1 = $fields -> lon1;
$con = DB::getConnection();
$query = "SELECT `lattitude`,`longitude` FROM venues";
$rs = mysqli_query($con,$query) or die (json_encode(array("result"=>-1, "message"=>mysqli_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)
{
$lat2 = $venue -> lattitude;
$lon2 = $venue -> longitude;
$dist = $this -> distance($lat1, $lon1, $lat2, $lon2);
$distances[] = $dist;
}
echo $lat1;
echo $lon1;
echo $lat2;
echo $lon2;
$result = array("lat1" => $lat1,"lon1" =>$lon1,"lat2" => $lat2 ,"lon2" => $lon2, "result"=>1, "message"=>"success", "distances" => $distances);
return json_encode($result);
} else {
$result = array("result"=>-1, "message"=>"Distances 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);
$lat1 = $data -> lattitude;
$lon1 = $data -> longitude;
echo $lat1;
echo $lon1;
$v = new Vendor();
$response = $v -> searchVendors($lat1,$lon1);
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);
}
}
?>
郵便配達員から渡される入力は次のとおりです。
{
"lat1" : "19.9764360",
"lon1" : "73.8573440"
}
私が得る出力は次のとおりです。
{
"lat1": null,
"lon1": null,
"lat2": null,
"lon2": null,
"result": 1,
"message": "success",
"distances": [
0,
0,
0,
0,
0,
0,
0,
0
]
}
編集:RigsFollyの回答による
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); // now its an object if it was stored as an object
$lat1 = $data -> lattitude;
$lon1 = $data -> longitude;
echo $lat1;
echo $lon1;
$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{
$con = DB::getConnection();
$query = "SELECT `lattitude`,`longitude` FROM venues";
$rs = mysqli_query($con,$query) or die (json_encode(array("result"=>-1, "message"=>mysqli_error())));
$n = mysqli_num_rows($rs);
$venues = array();
$lat1 = $fields -> lattitude;
$lon1 = $fields -> longitude;
if ( $n > 0 ) {
while ( $row = mysqli_fetch_object($rs)) {
$venues[] = $row;
}
$distances = array();
foreach($venues as $venue)
{
$lat2 = $venue -> lattitude;
$lon2 = $venue -> longitude;
$dist = $this -> distance($lat1, $lon1, $lat2, $lon2);
$distances[] = $dist;
}
echo $lat1;
echo $lon1;
echo $lat2;
echo $lon2;
$result = array("lat1" => $lat1,"lon1" =>$lon1,"lat2" => $lat2 ,"lon2" => $lon2, "result"=>1, "message"=>"success", "distances" => $distances);
return json_encode($result);
} else {
$result = array("result"=>-1, "message"=>"Distances list is empty");
return json_encode($result);
}
} catch(DBConnectionException $e) {
$result = array("result"=>-1, "message"=> $e -> getMessage());
return json_encode($result);
}
return null;
}
次のような出力が得られます。
{
"lat1": null,
"lon1": null,
"lat2": "-2.6357434",
"lon2": "18.4276047",
"result": 1,
"message": "success",
"distances": [
8321.16,
8322.48,
8321.37,
2617.96,
0,
2535.52,
3368.05,
2069.09
]
}
lat1,lon1 はまだ空のようですが、距離は正しいですか? どうすればキロ単位で取得できますか?
距離関数:
public function distance($lat1, $lon1, $lat2, $lon2) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
$km = $miles * 1.609344;
$result = round($km ,2);
return $result;
}
ここで何が問題なのですか?誰でも助けてもらえますか?ありがとうございました..