After extensive research and fun learning about Google's mapping api, I am building a digital antenna map application. At this point, the next step in the project plan is to build a memory-map replication of the google map that is locating digital tv stations based upon input from the user's address. So, I am using this math code to formulate the bearing of the center of the google map and a lat/lng point from the database result set.

My question is: How can I complete the math to show the bearing in degrees?

this code is the math that returns the following array result set:

```
1.21
1.10
1.10
1.10
1.10
2.62
-0.29
-1.17
0.12
3.04
var y = Math.sin(longitude-center.lng()) * Math.cos(latitude);
var x = Math.cos(center.lat())*Math.sin(latitude) - Math.sin(center.lat())*Math.cos(latitude)*Math.cos(longitude-center.lng());
var bearing = (Math.atan2(y, x)).toFixed(2);
```

There seems to something missing in my calculations. The db table is holding the longitude values as a negative number to represent the upper western quadrant of the globe.

Any suggestions to complete my math would save a million nuerons, I've already burned a trillion.

Taking the degrees to radian suggestion, I've modified the javascript code:

```
var radLat1 = center.lat() * Math.PI / 180;
var radLat2 = latitude * Math.PI / 180;
var radLng1 = center.lng() * Math.PI / 180;
var radLng2 = longitude * Math.PI / 180;
var y = Math.sin(radLng2- radLng1) * Math.cos(radLng2);
var x = Math.cos(radLat1)*Math.sin(radLat2) - Math.sin(radLat1)*Math.cos(radLat2)*Math.cos(radLng2-radLng1);
var bearing = (Math.atan2(y, x)).toFixed(2);
```

url to testing project site: click here

the div at the bottom of the page is a result set returned from 2 arrays, the first holding the distance and second holding the bearing measurement.

Google maps returns coordinates in degrees. Of course, trig functions expect radians. So you'll want to convert to radians first:

```
function deg_to_rad(degrees) {
return degrees * Math.PI / 180;
}
```

Your math looks like it's trying to do the the Haversine formula. I found a javascript implementation here for you to check your code against.

You can check your results against the FCC's DTV engineering maps.

With the help of Seth, and using my early morning brain (yes, I'm middle-aged and I am smarter in the morning).

The link to here, which I was studying, and Seth referred to, is the key. I used the code from the js file on the blog entry and morphed the code away from an oo javascript concept to a script running inline.

this is my solution:

```
var lat1 = center.lat();
var lon1 = center.lng();
var lat2 = latitude;
var lon2 = longitude;
lat1 = lat1 * Math.PI / 180;
lat2 = lat2 * Math.PI / 180;
var dLon = (lon2-lon1) * Math.PI / 180;
var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
var bearing = Math.atan2(y, x) * 180 / Math.PI;
if (bearing < 0){
bearing = bearing + 360;
}
bearing = bearing.toFixed(0);
stationDistance.push(distance.toFixed(1));
stationBearing.push(bearing);
```

I am running the demo google map here.

Thanks for the help and now I can move onto logic code that will recommend the correct product for my client's customer at this url.

stackoverflow ROCKS....

Robert

©2020 All rights reserved.