PHP uses the Baidu API to compute the distance between the two codes

Source: Internet
Author: User

Currently in the making of a dating project, you need to know the distance between two users. Baidu a bit, the operation is as follows:
The easiest way for us to get to the user's location is IP.
We have access to the user through the Baidu API latitude and longitude, the use of IP access to latitude and longitude API:

Http://developer.baidu.com/map/index.php?title=webapi/ip-api

After the latitude and longitude of two users can calculate the distance between two users. The calculations are as follows:

The code is as follows Copy Code
/**
* @desc calculate distances based on latitude and longitude between two points
* @param float $lat latitude value
* $LNG Longitude value of @param float
*/
function Getdistance ($lat 1, $lng 1, $lat 2, $LNG 2)
{
$earthRadius = 6367000; Approximate radius of Earth in meters


$lat 1 = ($lat 1 * PI ())/180;
$LNG 1 = ($lng 1 * PI ())/180;

$lat 2 = ($lat 2 * PI ())/180;
$LNG 2 = ($lng 2 * PI ())/180;


$calcLongitude = $lng 2-$LNG 1;
$calcLatitude = $lat 2-$lat 1;
$stepOne = Pow (sin ($calcLatitude/2), 2) + cos ($lat 1) * cos ($lat 2) * POW (sin ($calcLongitude/2), 2);
$STEPTWO = 2 * ASIN (MIN (1, sqrt ($stepOne));
$calculatedDistance = $earthRadius * $STEPTWO;

Return round ($calculatedDistance);
}

The code is untested. After testing, update this article to inform the results.

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.