PHP has latitude and longitude calculation distance

Source: Internet
Author: User

/** *  @desc 根据两点间的经纬度计算距离 *  @param float $lat 纬度值 *  @param float $lng 经度值 */   function getDistance( $lat1 $lng1 $lat2 $lng2 )   {       $earthRadius = 6367000;  //approximate radius of earth in meters       /*         Convert these degrees to radians         to work with the formula       */       $lat1 = ( $lat1 * pi() ) / 180;       $lng1 = ( $lng1 * pi() ) / 180;       $lat2 = ( $lat2 * pi() ) / 180;       $lng2 = ( $lng2 * pi() ) / 180;       /*         Using the         Haversine formula         http://en.wikipedia.org/wiki/Haversine_formula         calculate the distance       */       $calcLongitude $lng2 $lng1 ;       $calcLatitude $lat2 $lat1 ;       $stepOne = pow(sin( $calcLatitude / 2), 2) +  cos ( $lat1 ) *  cos ( $lat2 ) * pow(sin( $calcLongitude / 2), 2);   $stepTwo = 2 * asin(min(1, sqrt( $stepOne )));       $calculatedDistance $earthRadius $stepTwo ;       return round ( $calculatedDistance );   }

With an online calculation of two-point distance address:

Http://www.nhc.noaa.gov/gccalc.shtml

Note: Original link: http://my.oschina.net/ekc/blog/84106

PHP has latitude and longitude calculation distance

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.