calculate distance

后端 未结 3 1321
春和景丽
春和景丽 2021-01-07 15:36

i am designing a recruitment database, and i need it to perform several tasks that all involve integrating a way of calculating distance:

1) calculate the distance t

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-07 16:29

    The distance between 2 points could be calculated with the following formula :

    6371*acos(cos(LatitudeA)*cos(LatitudeB)*cos(longitudeB-longitudeA)+sin(LatitudeA)*sin(latitudeB))
    

    Of course it's a "crow flies" approximation in Km.

    Wich can be translated to php by :

    $longA     = 2.3458*(M_PI/180); // M_PI is a php constant
    $latA     = 48.8608*(M_PI/180);
    $longB     = 5.0356*(M_PI/180);
    $latB     = 47.3225*(M_PI/180);
    
    $subBA       = bcsub ($longB, $longA, 20);
    $cosLatA     = cos($latA);
    $cosLatB     = cos($latB);
    $sinLatA     = sin($latA);
    $sinLatB     = sin($latB);
    
    $distance = 6371*acos($cosLatA*$cosLatB*cos($subBA)+$sinLatA*$sinLatB);
    echo $distance ;
    

    With that you could compute the distance between two points (people) and of course determine if a point is in a radius of an other.

提交回复
热议问题