: This article describes how to calculate the distance between two GPS longitude and latitude coordinates in PHP. For more information about PHP tutorials, see. You can use your own code directly.
/*** 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 latitude and longitude difference $ 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);}/*** convert the Baidu coordinate system to the standard GPS coordinate system * @ param float $ lnglat coordinate system (for example, 106.426, 29.553404) * @ return string 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-$ by; return $ GPS_x. ','. $ GPS_y; // longitude, latitude} elsereturn $ lnglat ;}
The above describes how to calculate the distance between two GPS longitude and latitude coordinates in PHP, including some content, and hope to be helpful to friends who are interested in PHP tutorials.