-1

I found this Java code to calculate the midpoint between two latitude and longitude: midpoint between two latitude and longitude

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));
}

Anyone can help me to convert this piece of code to Borland Delphi?

With the input like this format:

lat1 = 48.865223, lon1 = 2.318974

lat2 = 48.865983, lon2 = 2.321475

I am not sure if that is already in "radian".

Community
  • 1
  • 1
Kawaii-Hachii
  • 1,017
  • 7
  • 22
  • 36

1 Answers1

1
lat1 = 48.865223, lon1 = 2.318974
lat2 = 48.865983, lon2 = 2.321475

These coordinates appear to be in degrees. The latitude is too large for radians. You can convert between degrees and radians using DegToRad and RadToDeg. Of course, it's not very hard to multiply and divide by π/180.

The trig functions, sin and cos can be found in the System unit, as can sqrt. For atan2 you need arctan2 from Math.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490