Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

java - calculating Lat and Long from Bearing and Distance

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:

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)+"
Longitude = "+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. :-)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...