PHP calculates the distance between two GPS coordinates on Baidu map-PHP source code

Source: Internet
Author: User
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;
}

Contact Us

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.