CakePHP query closest latitude longitude from database

后端 未结 5 536
轻奢々
轻奢々 2021-01-02 02:04

In a CakePHP (v3) application, how can I retrieve the closest results based on passed lat lng values?

I\'d like to have them back as native CakePHP entities, so some

5条回答
  •  春和景丽
    2021-01-02 02:42

    finally i got the solution for this. below is the working example

    $distance = 25;
    $start_latitude = 12.920479;
    $start_longitude = 77.670547;
    
    $contents = $this->Sightings->find()->select(['latitude', 'longitude', 'distance' => 'SQRT(
        POW(69.1 * (latitude - '.$start_latitude.'), 2) +
        POW(69.1 * ('.$start_longitude.' - longitude) * COS(latitude / 57.3), 2))'])->having(["distance < " => $distance]);
    

    output

       {"lat": xx.920479,
        "lng": xx.670547,
        "distance": "0"
       }
    

提交回复
热议问题