Haversine formuła z php

Chcę użyć tej formuły z php. Mam bazę danych z zapisanymi wartościami szerokości i długości geograficznej.

Chcę znaleźć, z określoną wartością szerokości i długości geograficznej w danych wejściowych, Wszystkie odległości (w km) od tego punktu z każdym punktem w bazie danych. Aby to zrobić, użyłem formuły na googlemaps api:

( 6371 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) )

Oczywiście używając tego w php zamieniłem radiany na deg2rad.Wartości 37, -122 to moje wartości wejściowe i lat,lng to moje wartości w baza danych.

Poniżej znajduje się mój kod. Problem w tym, że coś jest nie tak, ale nie rozumiem co. Wartość odległości jest oczywiście błędna.

//values of latitude and longitute in input (Rome - eur, IT)
$center_lat = "41.8350";
$center_lng =  "12.470";

//connection to database. it works
(..)

//to take each value in the database:
    $query = "SELECT * FROM Dati";
    $result = mysql_query($query);
    while ($row = @mysql_fetch_assoc($result)){
        $lat=$row['Lat']);
        $lng=$row['Lng']);
    $distance =( 6371 * acos((cos(deg2rad($center_lat)) ) * (cos(deg2rad($lat))) * (cos(deg2rad($lng) - deg2rad($center_lng)) )+ ((sin(deg2rad($center_lat))) * (sin(deg2rad($lat))))) );
    }

Dla wartości na przykład: $lat= 41.9133741000 $lng= 12.5203944000

Mam wyjście odległości= "4826.9341106926"

Author: martinstoeckli, 2013-02-07

5 answers

Formuła, której użyłeś, wydaje się być arccosine zamiast haversine . Wzór haversine ' a jest rzeczywiście bardziej odpowiedni do obliczania odległości na kuli, ponieważ jest mniej podatny na błędy zaokrąglania.

/**
 * Calculates the great-circle distance between two points, with
 * the Haversine formula.
 * @param float $latitudeFrom Latitude of start point in [deg decimal]
 * @param float $longitudeFrom Longitude of start point in [deg decimal]
 * @param float $latitudeTo Latitude of target point in [deg decimal]
 * @param float $longitudeTo Longitude of target point in [deg decimal]
 * @param float $earthRadius Mean earth radius in [m]
 * @return float Distance between points in [m] (same as earthRadius)
 */
function haversineGreatCircleDistance(
  $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
  // convert from degrees to radians
  $latFrom = deg2rad($latitudeFrom);
  $lonFrom = deg2rad($longitudeFrom);
  $latTo = deg2rad($latitudeTo);
  $lonTo = deg2rad($longitudeTo);

  $latDelta = $latTo - $latFrom;
  $lonDelta = $lonTo - $lonFrom;

  $angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
    cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
  return $angle * $earthRadius;
}

P. S. nie mogłem znaleźć błędu w Twoim kodzie, więc czy to tylko literówka, którą napisałeś $lat= 41.9133741000 $lat= 12.5203944000 ? Może po prostu obliczyłeś z $lat=12.5203944000 i $ long=0, ponieważ nadpisałeś zmienną $lat.

Edit:

Przetestowałem kod i zwrócił poprawny wynik:

$center_lat = 41.8350;
$center_lng = 12.470;
$lat = 41.9133741000;
$lng = 12.5203944000;

// test with your arccosine formula
$distance =( 6371 * acos((cos(deg2rad($center_lat)) ) * (cos(deg2rad($lat))) * (cos(deg2rad($lng) - deg2rad($center_lng)) )+ ((sin(deg2rad($center_lat))) * (sin(deg2rad($lat))))) );
print($distance); // prints 9.662174538188

// test with my haversine formula
$distance = haversineGreatCircleDistance($center_lat, $center_lng, $lat, $lng, 6371);
print($distance); // prints 9.6621745381693
 55
Author: martinstoeckli,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-11-20 08:35:49
public function getDistanceBetweenTwoPoints($point1 , $point2){
    // array of lat-long i.e  $point1 = [lat,long]
    $earthRadius = 6371;  // earth radius in km
    $point1Lat = $point1[0];
    $point2Lat =$point2[0];
    $deltaLat = deg2rad($point2Lat - $point1Lat);
    $point1Long =$point1[1];
    $point2Long =$point2[1];
    $deltaLong = deg2rad($point2Long - $point1Long);
    $a = sin($deltaLat/2) * sin($deltaLat/2) + cos(deg2rad($point1Lat)) * cos(deg2rad($point2Lat)) * sin($deltaLong/2) * sin($deltaLong/2);
    $c = 2 * atan2(sqrt($a), sqrt(1-$a));

    $distance = $earthRadius * $c;
    return $distance;    // in km
}
 5
Author: Tayyab Hussain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-08 07:09:31

From this link :

function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {
    $earth_radius = 6371;

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

Jak widzisz istnieje wiele różnic między tym kodem. Nie wiem, czy masz albo inne podejście do Formuły, albo może jakiś krok podczas konwersji do PHP poszedł źle, ale powyższa formuła powinna działać.

 4
Author: Naryl,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-07 11:48:52

Obliczam odległości proste wewnątrz zapytań, używając następującej procedury składowanej:

CREATE FUNCTION GEODIST (lat1 DOUBLE, lon1 DOUBLE, lat2 DOUBLE, lon2 DOUBLE)
    RETURNS DOUBLE
    DETERMINISTIC
        BEGIN
            DECLARE dist DOUBLE;
            SET dist =  round(acos(cos(radians(lat1))*cos(radians(lon1))*cos(radians(lat2))*cos(radians(lon2)) + cos(radians(lat1))*sin(radians(lon1))*cos(radians(lat2))*sin(radians(lon2)) + sin(radians(lat1))*sin(radians(lat2))) * 6378.8, 1);
            RETURN dist;
        END|

Wykonujesz powyższe jako polecenie SQl z poziomu phpMyAdmin, aby utworzyć procedurę. Po prostu zwróć uwagę na zakończenie|, więc w oknie wprowadzania SQL wybierz znak / jako ogranicznik.

Następnie w zapytaniu, nazwij to tak:

$sql = "
SELECT `locations`.`name`, GEODIST(`locations`.`lat`, `locations`.`lon`, " . $lat_to_calculate . ", " . $lon_to_calculate . ") AS `distance`
FROM `locations` ";

Okazało się, że jest to o wiele szybsze niż Obliczanie w PHP po uruchomieniu zapytania.

 1
Author: CyberBrain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-07 12:06:33

Robię klasę haversine, która ma statyczne fuction getDistance posiada cztery paramtery i zwraca odległość od punktów lokalizacji bota

class HaverSign {
    
     public static function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {
        $earth_radius = 6371;

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

Powyższa klasa jest przechowywana w katalogu głównym, a Katalog główny zawiera folder klas Wywołanie tego za pomocą następującego sposobu na dowolnej stronie php

include "../classes/HaverSign.php";
$haversign=new HaverSign();

$lat=18.5204;
$lon=73.8567;

$lat1=18.5404;
$lon1=73.8167;

$dist = $haversign->getDistance($lat,$lon,$lat1,$lon1);
echo $dist;

Wyjście jest następujące

4.7676529976827
 1
Author: Sandip Bhoi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2021-01-29 17:35:39