How to calculate the distance between two GPS coordinates in Baidu map using PHP?

Source: Internet
Author: User
PHP calculates the distance between two GPS coordinates of Baidu Map, gps coordinates. PHP calculates the distance between two GPS coordinates on Baidu Map. This document describes how PHP calculates the distance between two gps coordinates on Baidu Map. Share it with you for your reference. PHP calculates the distance between two GPS coordinates on Baidu Map.

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.

The following example illustrates how PHP calculates the distance between two GPS coordinates on Baidu Map. Share it with you for your reference ....

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.