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.