HTML5 obtains the user's latitude and longitude and distance calculation formula

Source: Internet
Author: User
Tags cos pow sin

The


needs to be positioned to get the latitude and longitude and calculate the distance when doing the project, using HTML5 to obtain the latitude and longitude degree.

<! doctype html> 

Calculation formula for distance between two degrees of latitude and longitude

/**  *  @desc   based on latitude and longitude between two points  *  @param  float  $lat   Latitude value  *  @param  float  $lng   Longitude value  */ public function getdistance ($lat 1,  $lng 1, $

lat2,  $lng 2)  {    $earthRadius  = 6367000;

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

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.