java 从方位角和距离计算纬度和经度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10119479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calculating Lat and Long from Bearing and Distance
提问by Abhendra Singh
I'm having a hard time wrapping my head around some Trigonometry. I am trying to deduce a destination latitude and longitude from a start lat and log and distance and bearing.
我很难用一些三角学来解决问题。我试图从开始纬度和日志以及距离和方位推断目的地纬度和经度。
Fortunately, I found an amazing site which describes exactly the function I need: http://www.movable-type.co.uk/scripts/latlong.html" Destination point given distance and bearing from start point " I tried it in my java program but it is not working for me. I deployed it as the website said. Here is my code:
幸运的是,我找到了一个很棒的网站,它准确地描述了我需要的功能:http: //www.movable-type.co.uk/scripts/latlong.html“从起点给定距离和方位的目的地点”我在我的java 程序,但它对我不起作用。我按照网站上说的部署了它。这是我的代码:
double dist = 150/6371;
double brng = Math.toRadians(90);
double lat1 = Math.toRadians(26.88288045572338);
double lon1 = Math.toRadians(75.78369140625);
double lat2 = Math.asin( Math.sin(lat1)*Math.cos(dist) + Math.cos(lat1)*Math.sin(dist)*Math.cos(brng) );
double a = Math.atan2(Math.sin(brng)*Math.sin(dist)*Math.cos(lat1), Math.cos(dist)-Math.sin(lat1)*Math.sin(lat2));
System.out.println("a = " + a);
double lon2 = lon1 + a;
lon2 = (lon2+ 3*Math.PI) % (2*Math.PI) - Math.PI;
System.out.println("Latitude = "+Math.toDegrees(lat2)+"\nLongitude = "+Math.toDegrees(lon2));
But it shows the output is:
但它显示输出是:
a = 0.0
Latitude = 26.882880455723377
Longitude = 75.78369140625
I am not getting where i am doing the mistake. Please anybody can help me to find out the problem.
我没有得到我做错的地方。请任何人都可以帮助我找出问题所在。
Thanx in Advance. :-)
提前谢谢。:-)
采纳答案by Simon Nickerson
Your problem is on your first line.
你的问题在你的第一行。
Try
尝试
double dist = 150.0 / 6371.0;
The reason is that 150/6371
gets calculated as 0
, because it performs integer division (rather than floating point division). This is true even though the result is being stored in a double
. You can force floating point division by making one of the two numbers a floating point literal.
原因是150/6371
计算为0
,因为它执行整数除法(而不是浮点除法)。即使结果存储在double
. 您可以通过将两个数字之一设为浮点文字来强制进行浮点除法。
回答by kiedysktos
if anyone needs a function that calculates point coordinates from other point moved by some distance, below is the working code. For me, it's just moving a point by some distance.
如果有人需要一个从移动了一定距离的其他点计算点坐标的函数,下面是工作代码。对我来说,它只是将一个点移动了一段距离。
import static java.lang.Math.*;
void movePoint(double latitude, double longitude, double distanceInMetres, double bearing) {
double brngRad = toRadians(bearing);
double latRad = toRadians(latitude);
double lonRad = toRadians(longitude);
int earthRadiusInMetres = 6371000;
double distFrac = distanceInMetres / earthRadiusInMetres;
double latitudeResult = asin(sin(latRad) * cos(distFrac) + cos(latRad) * sin(distFrac) * cos(brngRad));
double a = atan2(sin(brngRad) * sin(distFrac) * cos(latRad), cos(distFrac) - sin(latRad) * sin(latitudeResult));
double longitudeResult = (lonRad + a + 3 * PI) % (2 * PI) - PI;
System.out.println("latitude: " + toDegrees(latitudeResult) + ", longitude: " + toDegrees(longitudeResult));
}
- latitude, longitude- entry point coordinates
- distanceInMetres- distance that you want to move the point by
- bearing- an angle, direction towards which you want to move the point. 0 is towards the North, 90 - East, 180 - South, 270 - West. And all between, i.e. 45 is North East.
- earthRadiusInMetres- Earth radius in metres.
- 纬度,经度- 入口点坐标
- distanceInMetres- 要移动点的距离
- 轴承- 一个角度,你想要移动点的方向。0 朝北,90 - 东,180 - 南,270 - 西。两者之间,即 45 是东北。
- earthRadiusInMetres- 以米为单位的地球半径。
You can change the radius to 6371 if you want to have the input in kilometres or to miles if you want to have input in miles.
如果要以公里为单位输入,可以将半径更改为 6371,如果要以英里为单位输入,则可以将半径更改为英里。