How to calculate distance from lat/long in php?

后端 未结 9 1383
余生分开走
余生分开走 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:37

    Try this query. I found this one when googling but forgot who created it

    SELECT a.*,
                3956 * 2 * ASIN(SQRT( POWER(SIN(($lat - lat) * pi()/180 / 2), 2) + COS($lat * pi()/180) * COS(lat * pi()/180) *
                POWER(SIN(($long - longi) * pi()/180 / 2), 2) )) as
                distance FROM table
                GROUP BY id HAVING distance <= 500 ORDER by distance ASC
    

    $lat and $long variable is the current position of user. lat and longi is the latitude and longitudle of entries

提交回复
热议问题