How to calculate distance from lat/long in php?

后端 未结 9 1389
余生分开走
余生分开走 2020-12-09 06:18

What I am trying to do is I have entries in the database which have a lat/long stored with them. I want to calculate the distance between users lat/long and entries lat/long

9条回答
  •  再見小時候
    2020-12-09 06:27

    Tested like 3 functions and 3 queries and only one showed good distance:

    In meters:

    SELECT *,
    (
        (((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) 
        * 1000
    ) as `distance`
    FROM `table`
    ORDER BY `distance` ASC
    

    In kilometers:

    SELECT *,
    (
        (((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) 
    ) as `distance`
    FROM `table`
    ORDER BY `distance` ASC
    

提交回复
热议问题