What formula to use to calculate a small distance

被刻印的时光 ゝ 提交于 2019-12-14 01:45:42

问题


Hy!

I need to calculate the distance between 2 GPS Points.

I read this question Formulas to Calculate Geo Proximity but i my english is too bad.

My Problem is that the 2 points are at most 1 km away. I need the most excatly formula because of the small distance

A example in PHP or pseudo code would be great


回答1:


See this page. It contains great-circle distance calculation functions for various programming languages.

In PHP:

function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {  
    $earth_radius = 6371;  // In the unit you want the result in.

    $dLat = deg2rad($latitude2 - $latitude1);  
    $dLon = deg2rad($longitude2 - $longitude1);  

    $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2);  
    $c = 2 * asin(sqrt($a));  
    $d = $earth_radius * $c;  

    return $d;  
}  



回答2:


function spherical_law_of_cosines($lat_1, $lon_1, $lat_2, $lon_2, $unit = 'mi')
{
    $distance = (3956 * acos(cos(deg2rad($lat_1)) * cos(deg2rad($lat_2)) * cos(deg2rad($lon_2) - deg2rad($lon_1)) + sin(deg2rad($lat_1)) * sin(deg2rad($lat_2))));

    if(strcasecmp($unit, 'mi') == 0 OR strcasecmp($unit, 'miles') == 0)
    {
        return $distance;
    }

    if(strcasecmp($unit, 'km') == 0 OR strcasecmp($unit, 'kilometres') == 0 OR strcasecmp($unit, 'kilometers') == 0)
    {
        return 1.609344 * $distance;
    }

    if(strcasecmp($unit, 'm') == 0 OR strcasecmp($unit, 'metres') == 0 OR strcasecmp($unit, 'meters') == 0
    )
    {
        return 1.609344 * 1000 * $distance;
    }

    if(strcasecmp($unit, 'y') == 0 OR strcasecmp($unit, 'yards') == 0)
    {
        return 1760 * $distance;
    }

    if(strcasecmp($unit, 'ft') == 0 OR strcasecmp($unit, 'feet') == 0)
    {
        return 1760 * 3 * $distance;
    }

    return $distance;
}


来源:https://stackoverflow.com/questions/9096548/what-formula-to-use-to-calculate-a-small-distance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!