Haversine公式与PHP

我想用这个公式与PHP。 我有一个数据库保存了经度和纬度的一些值。

我想用input的经度和纬度的某个值来查找从这个点到数据库中每个点的所有距离(以km为单位)。 为此,我使用googlemaps api上的公式:

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

当然,在php中,我用deg2radreplace了弧度。值37,-122是我的input值和纬度值,lng是我在数据库中的值。

下面是我的代码。 问题是,有什么问题,但我不明白是什么。 距离的价值当然是错误的。

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

对于值例如:$ lat = 41.9133741000 $ lng = 12.5203​​944000

我有输出的距离=“4826.9341106926”

你使用的公式似乎是反余弦公式,而不是反余弦公式。 半正式公式确实更适合于计算球体上的距离,因为它不容易出现对数点的舍入误差。

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

PS我找不到你的代码中的错误,所以它只是一个错字,你写了$lat= 41.9133741000 $lat= 12.5203944000 ? 也许你只是用$ lat = 12.5203​​944000和$ long = 0来计算,因为你覆盖了你的$ latvariables。

编辑:

testing了代码,它返回了一个正确的结果:

 $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 
 public function getDistanceBetweenTwoPoints($point1 , $point2){ // array of lat-long ie $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 } 

从这个链接 :

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

正如你所看到的,这个代码有很多不同之处。 我不知道如果你有一个不同的方法来公式或可能转换到PHP出错的一些步骤,但上述公式应该工作。

我使用以下存储过程直接计算查询内的距离:

 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| 

您只需在phpMyAdmin中执行上面的SQl语句来创build过程。 只要注意到结尾|,所以在你的SQLinput窗口中select| 签署限制。

然后在查询中,像这样调用它:

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

查询运行后,我发现这比用PHP计算速度快得多。

我使具有四个参数的具有静态functiongetDistance的haversign类返回从bot位置点开始的距离

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

上面的类存储在根目录下,而根目录下包含classes文件夹在任何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; 

输出如下

 4.7676529976827