PHP to calculate the distance between the coordinates of the Baidu map _php tips

Source: Internet
Author: User
Tags cos php database php define php regular expression pow sin

This article describes the PHP to achieve the calculation of Baidu map coordinates the distance between the method. Share to everyone for your reference, specific as follows:

The following is the code on the Web, you need to make some changes when you use it

The first function is to obtain the range, the parameter is the latitude longitude radius

The second function is to compute the coordinate distance

<?php define (' PI ', 3.1415926535898);
Define (' Earth_radius ', 6378.137);
  The range of calculations can be done by searching user function GetRange ($lat, $lon, $raidus) {//computed latitude $degree = (24901 * 1609)/360.0;
  $dpmLat = 1/$degree;
  $radiusLat = $dpmLat * $raidus; $minLat = $lat-$radiusLat; Get the minimum latitude $maxLat = $lat + $radiusLat;
  The maximum latitude/longitude is computed $mpdLng = $degree * cos ($lat * (pi/180));
  $dpmLng = 1/$mpdLng;
  $radiusLng = $dpmLng * $raidus; $minLng = $lon-$radiusLng; To obtain the minimum longitude $maxLng = $lon + $radiusLng;
    Get maximum longitude/range $range = Array (' Minlat ' => $minLat, ' Maxlat ' => $maxLat, ' Minlon ' => $minLng,
  ' Maxlon ' => $maxLng);
return $range;
  //Get Distance function getdistance between 2 points ($lat 1, $lng 1, $lat 2, $lng 2) {$radLat 1 = $lat 1 * (pi/180);
  $radLat 2 = $lat 2 * (pi/180);
  $a = $radLat 1-$radLat 2;
  $b = ($lng 1 * (pi/180))-($lng 2 * (pi/180));
  $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 * 10000)/10000;
return $s;

 }

For more information on PHP related content readers can view the site topics: "PHP array" Operation Techniques Encyclopedia, "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage Summary", "Php+ajax Tips and Application Summary", "PHP operation and operator Usage Summary", " PHP Network Programming Skills Summary, "Introduction to PHP Basic Grammar", "PHP date and Time usage summary", "PHP object-oriented Programming Introductory Course", "PHP string (String) Usage Summary", "Php+mysql database Operation Tutorial" and " A summary of common PHP database operations tips

I hope this article will help you with the PHP program design.

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.