1<?php
2
3/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
4/*:: :*/
5/*:: This routine calculates the distance between two points (given the :*/
6/*:: latitude/longitude of those points). It is being used to calculate :*/
7/*:: the distance between two locations using GeoDataSource(TM) Products :*/
8/*:: :*/
9/*:: Definitions: :*/
10/*:: South latitudes are negative, east longitudes are positive :*/
11/*:: :*/
12/*:: Passed to function: :*/
13/*:: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) :*/
14/*:: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) :*/
15/*:: unit = the unit you desire for results :*/
16/*:: where: 'M' is statute miles (default) :*/
17/*:: 'K' is kilometers :*/
18/*:: 'N' is nautical miles :*/
19/*:: Worldwide cities and other features databases with latitude longitude :*/
20/*:: are available at https://www.geodatasource.com :*/
21/*:: :*/
22/*:: For enquiries, please contact sales@geodatasource.com :*/
23/*:: :*/
24/*:: Official Web site: https://www.geodatasource.com :*/
25/*:: :*/
26/*:: GeoDataSource.com (C) All Rights Reserved 2018 :*/
27/*:: :*/
28/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
29function distance($lat1, $lon1, $lat2, $lon2, $unit) {
30 if (($lat1 == $lat2) && ($lon1 == $lon2)) {
31 return 0;
32 }
33 else {
34 $theta = $lon1 - $lon2;
35 $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
36 $dist = acos($dist);
37 $dist = rad2deg($dist);
38 $miles = $dist * 60 * 1.1515;
39 $unit = strtoupper($unit);
40
41 if ($unit == "K") {
42 return ($miles * 1.609344);
43 } else if ($unit == "N") {
44 return ($miles * 0.8684);
45 } else {
46 return $miles;
47 }
48 }
49}
50
51echo distance(32.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles<br>";
52echo distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers<br>";
53echo distance(32.9697, -96.80322, 29.46786, -98.53506, "N") . " Nautical Miles<br>";
54
55?>