This article describes how to use PHP to calculate the distance between two GPS coordinates of Baidu map. It is a typical application developed for Baidu map interface. For more information, see
This article describes how to use PHP to calculate the distance between two GPS coordinates of Baidu map. It is a typical application developed for Baidu map interface. For more information, see
This article describes how to calculate the distance between two GPS coordinates in Baidu map using PHP. Share it with you for your reference.
The specific implementation method is as follows:
The Code is as follows:
/**
* Calculate the distance between two coordinates (meters)
* @ Param float $ fP1Lat start point (latitude)
* @ Param float $ fP1Lon start point (longitude)
* @ Param float $ fP2Lat end point (latitude)
* @ Param float $ fP2Lon end point (longitude)
* @ Return int
*/
Function distanceBetween ($ fP1Lat, $ fP1Lon, $ fP2Lat, $ fP2Lon ){
$ Fear_radius = 6378137;
// Converts degrees to radians.
$ FRadLon1 = deg 2rad ($ fP1Lon );
$ FRadLon2 = deg 2rad ($ fP2Lon );
$ FRadLat1 = deg 2rad ($ fP1Lat );
$ FRadLat2 = deg 2rad ($ fP2Lat );
// Calculate the difference between latitude and longitude
$ 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 ($ fear_radius * 2 * asin (sqrt ($ fP) + 0.5 );
}
/**
* Baidu coordinate system converted to standard GPS coordinate system
* @ Param float $ lnglat coordinates (e.g., 106.426, 29.553404)
* @ Return string the standard GPS value after conversion:
*/
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-$;
Return $ GPS_x. ','. $ GPS_y; // longitude, latitude
} Else
Return $ lnglat;
}
I hope this article will help you with php programming.
,