How can we use php to calculate the distance between two GPS longitude and latitude coordinates when using Baidu map? Now we will share the function code with you. How can we use php to calculate the distance between two GPS longitude and latitude coordinates when using Baidu map? Now we will share the function code with you.
Script ec (2); script
The following is my PHP code used to calculate the distance between two GPS longitude and latitude coordinates. If you need it, you can use it directly.
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; } |