PHP calculates distance _php skills based on latitude and longitude between two points

Source: Internet
Author: User

This is a good example, directly affixed to the code, first of all to know the latitude value, longitude value

/** 
* @desc calculates the distance * @param float $lat latitude value based on the longitude of two points * 
@param float $lng Longitude value * 
/function getdistance ($la T1, $LNG 1, $lat 2, $lng 2) 
{ 
$earthRadius = 6367000;//approximate radius of Earth in meters/ 

* 
Convert thes e degrees to radians to work with the 
formula 
* 

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

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

/* 
Using the 
haversine formula 

http://en.wikipedia.org/wiki/Haversine_formula 

Calculate the Distance 
* * 

$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); 
 

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.