Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

midpoint between two latitude and longitude

Tags:

java

math

maps

I am trying to convert the code snippet given in this http://www.movable-type.co.uk/scripts/latlong.html into java. But I am not getting same result as that of site. Here is my code to find the midpoint between two points where their latitudes and longitudes are given

midPoint(12.870672,77.658964,12.974831,77.60935);     public static void midPoint(double lat1,double lon1,double lat2,double lon2)     {    double dLon = Math.toRadians(lon2-lon1);         double Bx = Math.cos(lat2) * Math.cos(dLon);         double By = Math.cos(lat2) * Math.sin(dLon);         double lat3 = Math.atan2(Math.sin(lat1)+Math.sin(lat2),Math.sqrt( (Math.cos(lat1)+Bx)*(Math.cos(lat1)+Bx) + By*By) );         double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);         System.out.print(lat3 +" " + lon3 );     } 

I am not sure whethe dLon is correct or not. So please help me guys to figure it out. P.S.I need to find the latitude and longitude of the midpoint

like image 596
CrazyCoder Avatar asked Jan 11 '11 10:01

CrazyCoder


People also ask

How do you find the midpoint between two points using latitude and longitude?

When given the end points of a line segment, you can find out its midpoint by using the midpoint formula. As the name might have already suggested, midpoint is basically the halfway between two end points. All you need to do is dividing the sum of x-values and the sum of y-values by 2.

How do you find the center of latitude and longitude?

The centroid of finitely many points is simply the arithmetic mean of each of the coordinates. So just sum up the latitudes and longitudes and divide by the number of points.

How do you find the midpoint of two pairs of coordinates?

Measure the distance between the two end points, and divide the result by 2. This distance from either end is the midpoint of that line. Alternatively, add the two x coordinates of the endpoints and divide by 2.

What is the distance between 2 latitudes?

What is the Distance Between Lines of Latitude? Lines of latitude are called parallels and in total there are 180 degrees of latitude. The distance between each degree of latitude is about 69 miles (110 kilometers).


1 Answers

You need to convert to radians. Change it to the following:

public static void midPoint(double lat1,double lon1,double lat2,double lon2){      double dLon = Math.toRadians(lon2 - lon1);      //convert to radians     lat1 = Math.toRadians(lat1);     lat2 = Math.toRadians(lat2);     lon1 = Math.toRadians(lon1);      double Bx = Math.cos(lat2) * Math.cos(dLon);     double By = Math.cos(lat2) * Math.sin(dLon);     double lat3 = Math.atan2(Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + Bx) * (Math.cos(lat1) + Bx) + By * By));     double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);      //print out in degrees     System.out.println(Math.toDegrees(lat3) + " " + Math.toDegrees(lon3)); } 
like image 197
dogbane Avatar answered Oct 08 '22 21:10

dogbane