PHP method to calculate the distance between two GPS coordinates on Baidu map, gps coordinates
The example in this article describes the method of PHP calculating the distance between two GPS coordinates on Baidu Map. Share it with everyone for your reference.
The specific implementation method is as follows:
Copy code The code is as follows:
/**
* Calculate the distance between two coordinates (meters)
* @param float $fP1Lat starting point (latitude)
* @param float $fP1Lon starting point (longitude)
* @param float $fP2Lat end point (latitude)
* @param float $fP2Lon end point (longitude)
* @return int
*/
function distanceBetween($fP1Lat, $fP1Lon, $fP2Lat, $fP2Lon){
$fEARTH_RADIUS = 6378137;
//Convert angles to radians
$fRadLon1 = deg2rad($fP1Lon);
$fRadLon2 = deg2rad($fP2Lon);
$fRadLat1 = deg2rad($fP1Lat);
$fRadLat2 = deg2rad($fP2Lat);
//Calculate the difference between longitude and latitude
$fD1 = abs($fRadLat1 - $fRadLat2);
$fD2 = abs($fRadLon1 - $fRadLon2);
//Distance calculation
$fP = pow(sin($fD1/2), 2) +
cos($fRadLat1) * cos($fRadLat2) * pow(sin($fD2/2), 2);
Return intval($fEARTH_RADIUS * 2 * asin(sqrt($fP)) + 0.5);
}
/**
* Convert Baidu coordinate system to standard GPS coordinate system
* @param float $lnglat coordinates (such as: 106.426, 29.553404)
* @return string converted standard GPS value:
*/
function BD09LLtoWGS84($fLng, $fLat){ // Longitude, latitude
$lnglat = explode(',', $lnglat);
List($x,$y) = $lnglat;
$Baidu_Server = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x={$x}&y={$y}";
$result = @file_get_contents($Baidu_Server);
$json = json_decode($result);
If($json->error == 0){
$bx = base64_decode($json->x);
$by = base64_decode($json->y);
$GPS_x = 2 * $x - $bx;
$GPS_y = 2 * $y - $by;
return $GPS_x.','.$GPS_y;//Longitude, latitude
}else
return $lnglat;
}
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/940496.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/940496.htmlTechArticleHow PHP calculates the distance between two GPS coordinates on Baidu map, gps coordinates This article describes the PHP calculation of Baidu map Method of distance between two GPS coordinates. Share it with everyone for your reference. ...