Calculates the straight distance based on the latitude and longitude of two points

Source: Internet
Author: User
Tags cos

<?PHP/** * @desc calculates distance according to latitude and longitude between two points * @param float $lat latitude value * @param float $lng Longitude value*/ functionGetdistance ($lat 1,$LNG 1,$lat 2,$LNG 2) {     $earthRadius= 6367000;//approximate radius of earth in meters     /*Convert These 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 Dist ance*/     $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); }     $lat 1= ' 36.8274190000 '; $LNG 1= ' 118.0484210000 '; $lat 2= ' 36.8419400000 '; $LNG 2= ' 118.0494970000 '; EchoGetdistance ($lat 1,$LNG 1,$lat 2,$LNG 2);

Calculates the straight distance based on the latitude and longitude of two points

Related Article

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.