映射数学和Javascript [英] Mapping Math and Javascript
问题描述
经过深入调研和有趣的学习谷歌的地图API,我建立一个数字天线地图应用。在这一点上,在项目计划中的下一个步骤是构建谷歌地图被定位基于来自用户的地址输入的数字电视台的存储器映射的复制。所以,我用这个数学code制订谷歌地图,并从数据库结果集的纬度/经度点中心的轴承。
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?
这code是返回下面的数组结果集数学:
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);
似乎有东西在我的计算中失踪。 db表是抱着经度值为负数重新present地球上象限西部
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.
任何建议,以完成我的数学会节省一百万nuerons,我已经烧了一万亿。
Any suggestions to complete my math would save a million nuerons, I've already burned a trillion.
服用度弧度的建议,我已经修改了JavaScript的code:
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来测试项目选址:点击这里
在页面底部的格为2阵列返回的结果集,第一保持的距离和第二保持轴承测量
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;
}
您的数学看起来像它试图做的haversine公式。我发现了一个JavaScript实现 的为您检查code反对。
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.
您可以检查您对 FCC的数字电视工程的地图。
这篇关于映射数学和Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!