# Calculate the distance between two GPS longitude and latitude coordinates. PHP

Source: Internet
Author: User

`/*** 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.

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.