问题描述
在对 Google 的地图 API 进行了广泛的研究和有趣的学习之后,我正在构建一个数字天线地图应用程序.此时,项目计划的下一步是构建谷歌地图的内存映射副本,该副本根据用户地址的输入定位数字电视台.所以,我使用这个数学代码来制定谷歌地图中心的方位和数据库结果集中的纬度/经度点.
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);
我的计算中似乎缺少一些东西.db 表将经度值保存为负数,以表示地球的上西象限.
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.
任何完成我的数学的建议都可以节省 100 万个神经元,我已经烧掉了 1 万亿个.
Any suggestions to complete my math would save a million nuerons, I've already burned a trillion.
将度数转换为弧度建议,我修改了 javascript 代码:
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);
测试项目站点的网址:点击此处
页面底部的div是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 地图以度数返回坐标.当然,三角函数需要弧度.因此,您首先要转换为弧度:
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 实现供您检查代码.
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 的 DTV 工程地图检查您的结果.
You can check your results against the FCC's DTV engineering maps.
这篇关于映射数学和 Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!