项目作者: AparajitaCoder

项目描述 :
The function will use the latitude and longitude of two locations, and calculate the distance between them in both miles and metric units.
高级语言: PHP
项目地址: git://github.com/AparajitaCoder/How-to-calculate-distance-between-two-points.git


How-to-calculate-distance-between-two-points

About

The function will use the latitude and longitude of two locations, and calculate the distance between them in both miles and metric units.

Usage

See the index.php file, or use this below:

  1. //get the distance between two points.
  2. function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) {
  3. $theta = $longitude1 - $longitude2;
  4. $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
  5. $miles = acos($miles);
  6. $miles = rad2deg($miles);
  7. $miles = $miles * 60 * 1.1515;
  8. $feet = $miles * 5280;
  9. $yards = $feet / 3;
  10. $kilometers = $miles * 1.609344;
  11. $meters = $kilometers * 1000;
  12. return compact('miles','feet','yards','kilometers','meters');
  13. }
  14. $point1 = array('lat' => 40.770623, 'long' => -73.964367);
  15. $point2 = array('lat' => 40.758224, 'long' => -73.917404);
  16. $distance = getDistanceBetweenPointsNew($point1['lat'], $point1['long'], $point2['lat'], $point2['long']);
  17. foreach ($distance as $unit => $value) {
  18. echo $unit.': '.number_format($value,4).'<br />';
  19. }