Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find center geopoint between start geo point and end geo point on android

Tags:

android

I have start latitude, longitude and end latitude,longitude.

I find Geo Point using latitude and longitude.

After that i draw line between the two Geo points.

My question is how to find the center of the Geopoint?

I display "OverlayItem" on start and end position. But unable to find the center of the Geopoint.

How to find center point?

i use below code for display overlayItem on start and end position.

        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]);
like image 871
RVG Avatar asked Jul 27 '12 05:07

RVG


1 Answers

You can use Haversine formula snippet and checked 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));
}
like image 151
Trung Nguyen Avatar answered Nov 12 '22 08:11

Trung Nguyen