<?php
function HeadingNew($lat1, $long1, $lat2, $long2) {
// Takes lat/longs as expressions in Degrees
$rLat1 = 0.0;
$rLong1 = 0.0;
$rLat2 = 0.0;
$rLong2 = 0.0;
$d = 0.0;
$h = 0.0;
$result = 0.0;
$rLat1 = deg2rad($lat1);
$rLong1 = deg2rad($long1);
$rLat2 = deg2rad($lat2);
$rLong2 = deg2rad($long2);
$d = deg2rad($long2 - $long1);
$y = sin($d) * cos($rlat2);
$x = cos($rlat1) * sin($rlat2) - sin($rlat1) * cos($rlat2) * cos($d);
$h = atan2($y, $x);
//echo "D = ".$d." X = ".$x." Y = ".$y." H = ".$h."<br>";
$result = rad2deg($h);
return($result);
}
function Heading($lat1, $long1, $lat2, $long2) {
// Takes lat/longs as expressions in Degrees
$pi = 3.14159265358979;
$rLat1 = 0.0;
$rLong1 = 0.0;
$rLat2 = 0.0;
$rLong2 = 0.0;
$d = 0.0;
$h = 0.0;
$result = 0.0;
$rLat1 = deg2rad($lat1);
$rLong1 = deg2rad($long1);
$rLat2 = deg2rad($lat2);
$rLong2 = deg2rad($long2);
// Distance in Meters Using Radians
// Less subject to rounding errors for small distances
$d = ( 2 * asin(sqrt(pow(sin(($rLat1 - $rLat2) / 2), 2) + cos($rLat1) * cos($rLat2) * pow(sin(($rLong1 - $rLong2) / 2),2))));
if (sin(ABS($rLong2) - ABS($rLong1)) < 0) {
//echo "top calc <br>";
$h = acos(((sin($rLat2) - sin($rLat1)) * cos($d)) / (sin($d) * cos($rLat1)));
}
else {
//echo "bottom calc <br>";
$h = 2 * $pi - acos(((sin($rLat2) - sin($rLat1)) * cos($d)) / (sin($d) * cos($rLat1)));
}
$result = rad2deg($h);
return($result);
}
$dHeading = number_format(Heading(30, -30, 30, -20));
echo "heading east in the northern/western hemisphere: ".$dHeading."<br>";
$dHeading = number_format(Heading(30, -20, 30, -30));
echo "heading west in the northern/western hemisphere: ".$dHeading."<br>";
$dHeading = number_format(Heading(30, -30, 40, -30));
echo "heading north in the northern/western hemisphere: ".$dHeading."<br>";
$dHeading = number_format(Heading(40, -30, 30, -30));
echo "heading south in the northern/western hemisphere: ".$dHeading."<p>";
$dHeading = number_format(Heading(30, 30, 30, 40));
echo "heading east in the northern/eastern hemisphere: ".$dHeading."<br>";
$dHeading = number_format(Heading(30, 40, 30, 30));
echo "heading west in the northern/eastern hemisphere: ".$dHeading."<br>";
$dHeading = number_format(Heading(30, 30, 40, 30));
echo "heading north in the northern/eastern hemisphere: ".$dHeading."<br>";
$dHeading = number_format(Heading(40, 30, 30, 30));
echo "heading south in the northern/eastern hemisphere: ".$dHeading."<p>";
$dHeading = number_format(Heading(-50, -50, -50, -40));
echo "heading east in the southern/western hemisphere: ".$dHeading."<br>";
$dHeading = number_format(Heading(-50, -40, -50, -50));
echo "heading west in the southern/western hemisphere: ".$dHeading."<br>";
$dHeading = number_format(Heading(-50, -50, -40, -50));
echo "heading north in the southern/western hemisphere: ".$dHeading."<br>";
$dHeading = number_format(Heading(-40, -50, -50, -50));
echo "heading south in the southern/western hemisphere: ".$dHeading."<p>";
$dHeading = number_format(Heading(-50, 50, -50, 60));
echo "heading east in the southern/eastern hemisphere: ".$dHeading."<br>";
$dHeading = number_format(Heading(-50, 60, -50, 50));
echo "heading west in the southern/eastern hemisphere: ".$dHeading."<br>";
$dHeading = number_format(Heading(-50, 50, -40, 50));
echo "heading north in the southern/eastern hemisphere: ".$dHeading."<br>";
$dHeading = number_format(Heading(-40, 50, -50, 50));
echo "heading south in the southern/eastern hemisphere: ".$dHeading."<br>";
?>
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
by: AlexanderRPosted on 2007-12-12 at 17:07:01ID: 20461584
This looks interesting.
I just don't really follow what exactly is done. You need user to input his current location and then his destination (using lang and lat) and it suppose to give him compass bearing direction to the destination from where he is now? Just asking, because that website deals with spherical distance.