I have a table of venues, which has column of latitude ,longitude, which states it's location. I want to get the locations from venues table and get the locations which are near to the current location of the user.
Current location would be given as a parameters to the function.
For this 1st I tried to get all the latitude and longitude from the venues table created an array of it.
Then I tried to calculate the distance for each venue in venues array. But its giving an error for the $distances array I have created.
I am new to php so no idea how to do this.
I want to return the locations which are near 10 km from the current location of the user.
search function :
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);
}
}
?>
Edit : search function
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;
}
getting output like this:
{
"result": 1,
"message": "success",
"distances": [
0,
0,
0,
0,
0,
0,
0,
0
]
}
How can I achieve this? What I should change in my code? Can anyone help please? Thank you..