PHP calculates the distance between two coordinates

Source: Internet
Author: User
<?phpdefine (' Earth_radius ', 6378.137);//Earth radius define (' PI ', 3.1415926);/** * Calculates the distance between two sets of latitude coordinates * PARAMS:LAT1 latitude 1; lng1 Longitude 1 ; lat2 latitude 2; lng2 longitude 2; Len_type (1:m or 2:km); * return m or km */function getdistance ($lat 1, $lng 1, $lat 2, $lng 2, $len _type = 1, $decimal = 2) {   $radLat 1 = $lat 1 * P i/180.0;   $radLat 2 = $lat 2 * pi/180.0;   $a         = $radLat 1-$radLat 2;   $b         = ($lng 1 * pi/180.0)-($lng 2 * pi/180.0);   $s = 2 * ASIN (sqrt (Pow (sin ($a/2), 2) + cos ($radLat 1) * cos ($radLat 2) * POW (sin ($b/2), 2)));   $s = $s * earth_radius;   $s = round ($s *);   if ($len _type > 1)   {       $s/=;   }      Return round ($s, $decimal);} Echo getdistance (39.908156,116.4767, 39.908452, 116.450479, 1);//output distance/m
  • 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.