Math and Javascript Display

After extensive research and fun learning the Google mapping api, I am building a digital antenna map application. At this stage, the next step in the project plan is to create a replication of the memory card on the google map, which houses digital television stations based on input from a user address. So, I use this math code to formulate a google map center support and a lat / lng point from a database result set.

My question is: how can I finish the math to show the bearing in degrees?

this code is math that returns the following array:

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); 

Something is missing in my calculations. Table db holds the longitude values ​​as a negative number to represent the upper western quadrant of the globe.

Any suggestions to finish the math would save a million nouons, I already burned a trillion.

Taking degrees to the radian sentence, I changed 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 for testing the project site: click here

The div at the bottom of the page is the result set returned from 2 arrays, the first of which holds the distance and the second holds the bearing dimension.

+5
source share
2 answers

Google maps return coordinates in degrees. Of course, trigger functions expect radians. So first you need to convert to radians first:

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

Your math looks like it is trying to make a Haversin formula . I found a javascript implementation here here so you can check your code.

You can check your results against FCC DTV technical cards .

+6
source

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

The link to here I studied and, according to Seth, is the key. I used the code from the js file in a blog post and turned the code away from the javascript concept from w to script executing an inline line.

this is my decision:

 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 a google demo map here .

Thanks for the help, and now I can switch to the logical code that will recommend the right product for my client to this URL .

stackoverflow ROCKS ....

Robert

+3
source

Source: https://habr.com/ru/post/922114/


All Articles