What formula to use to calculate a small distance

2019-07-06 05:30发布

问题:

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;
}