0

I found this Stack Overflow question.

Distance in meters between two Spacial Points in MySQL query

I want to implement it for my use case but I get this error:

1210 - #1210 Incorrect arguments to st_distance_sphere.

I have a database table with these columns:

  • CompanyObjectId
  • CompanyName
  • CityName
  • Latitude
  • Longitude
  • CampingLocationType
  • PhotoUrl

How can I take the Latitude and Longitude from each row inside tbl_cc table and calculate a result that has distance in meters which is less than or equal 300 meters?

SELECT CompanyObjectId, CompanyName, CityName, Latitude, Longitude,
       CampingLocationType, PhotoUrl
FROM tbl_cc
WHERE st_distance_sphere(POINT(13.5145461,43.564368), POINT(Longitude, Latitude)) <= 300

I also tried to do this query but got the same error:

SELECT * FROM (SELECT CompanyObjectId, CompanyName, CityName, Latitude, Longitude,
       CampingLocationType, PhotoUrl, (st_distance_sphere(POINT(13.5145461,43.564368), POINT(Longitude, Latitude))) AS MetriDaPosizione
FROM tbl_camper_contact) AS Q WHERE MetriDaPosizione <= 300

****UPDATE****

Now where I execute my script in php it generates this query sql for including meters:

SELECT CompanyObjectId, CompanyName, CityName, Latitude, Longitude,
                  CampingLocationType, PhotoUrl, st_distance_sphere(point(13.5129324,43.5578855), point(CAST(Longitude AS DECIMAL(18,14)), CAST(Latitude AS DECIMAL(18,14))), 100000) as Metri
                  FROM tbl_camper_contact
            WHERE st_distance_sphere(point(13.5129324,43.5578855), point(CAST(Longitude AS DECIMAL(18,14)), CAST(Latitude AS DECIMAL(18,14))), 100000)

In mysql admin it's working but in php I receive this error that make me crazy:

Mon, 16 Dec 2019 16:14:26 GMT [error] [client 93.36.55.43] - www.testcolan.it - AH01215: PHP Warning: mysqli_query(): (HY000/1210): Incorrect arguments to st_distance_sphere in /web/htdocs/www.testcolan.it/home/campit/rest_api/sql/DBOperations.php on line 759: /web/htdocs/www.testcolan.it/home/campit/rest_api/getListaDoveSostare.php Mon, 16 Dec 2019 16:14:26 GMT [error] [client 93.36.55.43] - www.testcolan.it - AH01215: PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /web/htdocs/www.testcolan.it/home/campit/rest_api/GeneralFunctions.php on line 242: /web/htdocs/www.testcolan.it/home/campit/rest_api/getListaDoveSostare.php

My script php is :

public function GetListaDoveSostare($lat, $lng, $kmmin, $ordinamentoData, $localita)
{
  $metri = (intval($kmmin) * 1000);

  $com = new DbConnect();
  $sql = "";


    $sql = "SELECT CompanyObjectId, CompanyName, CityName, Latitude, Longitude,
                  CampingLocationType, PhotoUrl, st_distance_sphere(point(".floatval($lng).",".floatval($lat)."), point(CAST(Longitude AS DECIMAL(18,14)), CAST(Latitude AS DECIMAL(18,14))), $metri) as Metri
                  FROM tbl_camper_contact
            WHERE st_distance_sphere(point(".floatval($lng).",".floatval($lat)."), point(CAST(Longitude AS DECIMAL(18,14)), CAST(Latitude AS DECIMAL(18,14))), $metri)";

  mysqli_set_charset($com->getDb(), 'utf8');
  $result = mysqli_query($com->getDb(), $sql);
  return $result;
}

And the return is putted inside function for generating json:

 public function GetListaDoveSostare($resultDoveSostare, $lat, $lng)
    {
      $dbOperationsObject = new DBOperations();
      $doveSostare= array();

        while ($row = mysqli_fetch_array($resultDoveSostare)) {

          $dove_sostare = array(
            "CompanyObjectId" => $row['CompanyObjectId'],
            "CompanyName" => $row['CompanyName'],
            "CityName" => $row['CityName'],
            "Latitude" => $row['Latitude'],
            "Longitude" => $row['Longitude'],
            "CampingLocationType" => ($row['CampingLocationType'] == "Camperplace") ? "Area Sosta" : "Campeggio",
            "PhotoUrl" => $row['PhotoUrl'],
            "MetriDaPosizioneCorrente" =>  $row['Metri']//$metri

          );

          $json= json_encode($dove_sostare);
          $dove_sostare = json_decode($json);
          array_push($doveSostare, $dove_sostare);
      }




      return $doveSostare;

    } 

Thanks very much for any helps
Cristian

1 Answers1

0

Looking at your first query, you are not passing the radius correctly:

SELECT CompanyObjectId, CompanyName, CityName, Latitude, Longitude,
       CampingLocationType, PhotoUrl
FROM tbl_cc
WHERE st_distance_sphere(POINT(13.5145461,43.564368), POINT(Longitude, Latitude)) <= 300

should be

SELECT CompanyObjectId, CompanyName, CityName, Latitude, Longitude,
       CampingLocationType, PhotoUrl
FROM tbl_cc
WHERE st_distance_sphere(POINT(13.5145461,43.564368), POINT(Longitude, Latitude), 300)

The error you mention is raised "if the radius argument is present but not positive". See this for further reference

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74