0

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..

Sid
  • 2,792
  • 9
  • 55
  • 111
  • 1
    Before getting into it too much, you're missing a semicolon after that `$distance = ...` statement. Does that help? – Don't Panic Aug 08 '16 at 20:59
  • 1
    I assume the error you're seeing tells you that PHP is not expecting `$distances`, (because the previous statement wasn't terminated). – Don't Panic Aug 08 '16 at 21:00
  • yes that was the problem but solving that i get null array could you please check edited code? @Don'tPanic – Sid Aug 08 '16 at 21:23

1 Answers1

0

You can work out the distance and order in the MySQL statement itself. You'll need to work out the Lat/Lon from the current address given by the user when searching, then use the following code:

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;
  • You could then also set the maximum distance from the searched address with something like this.. `HAVING distance < 50` (50 miles) – Adam Collins Aug 08 '16 at 20:17