Find the geodetic center point between the start point and end point of the geodesy on android

I have the initial latitude, longitude and final latitude, longitude.

I find Geo Point using latitude and longitude.

After that, I draw a line between the two points of the Geo.

My question is how to find a Geopoint center?

I show "OverlayItem" in the start and end positions. But could not find the center of Grozny.

How to find the center point?

I am using the code below to display overlayItem in the start and end positions.

        drawable            = getResources().getDrawable(R.drawable.annot_start);
        point               = new GeoPoint((int)(startLatitude*1E6), (int)(startLongitude*1E6));
        overlayItem         = new OverlayItem(point, startAddress, "Welcome");
        itemizedOverlay[n]= new MyItemizedOverlay(drawable, mapView);
        itemizedOverlay[n].addOverlay(overlayItem);
        mapOverlays.add(itemizedOverlay[n]);

        drawable            = getResources().getDrawable(R.drawable.annot_end);
        point               = new GeoPoint((int)(endLatitude*1E6), (int)(endLongitude*1E6));
        overlayItem         = new OverlayItem(point, endAddress, "Welcome");
        itemizedOverlay[n+1]= new MyItemizedOverlay(drawable, mapView);
        itemizedOverlay[n+1].addOverlay(overlayItem);
        mapOverlays.add(itemizedOverlay[n+1]);
+5
source share
3 answers

You can use the Haversine formulasnippet and check with this page.

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));
}
+8
source

The proven solution does not work for me.

Anyway, I found the simplest solution:

   private GeoPoint midPoint(double lat1,double lon1,double lat2,double lon2){
        //create origin geopoint from parameters
        GeoPoint origin = new GeoPoint(lat1,lon1);
        //create destination geopoints from parameters
        GeoPoint destination = new GeoPoint(lat2,lon2);
        //calculate and return center
        return GeoPoint.fromCenterBetween(origin, destination);
    }

GeoPoint, "GeoPoint.fromCenterBetween".

+1

You can approximate the midpoint by taking the average latitude of two points and the average longitude.

If you need it a little more accurately, you can use the Haversin formula and some algebra to get the midpoint.

0
source

All Articles