Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get center point(LatLng) between some locations?

Anyone can help me get center point. I have 6 LatLng objects, now i need to get a LatLng object is center. Many thanks!

like image 646
Na Pro Avatar asked Feb 26 '16 06:02

Na Pro


2 Answers

You need to calculate the centroid of the polygon defined by your points. Wikipedia defines the centroid as:

The centroid or geometric center of a plane figure is the arithmetic mean ("average") position of all the points in the shape

To calculate the centroid of a finite set of points you can use the following method:

private LatLng computeCentroid(List<LatLng> points) {
    double latitude = 0;
    double longitude = 0;
    int n = points.size();

    for (LatLng point : points) {
        latitude += point.latitude;
        longitude += point.longitude;
    }

    return new LatLng(latitude/n, longitude/n);
}
like image 177
antonio Avatar answered Sep 22 '22 22:09

antonio


public static void midPoint(double lat1,double lon1,double lat2,double lon2){

    double dLon = Math.toRadians(lon2 - lon1);


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


}

lat3 and lon3 are midpoints

like image 29
mr. pc_coder Avatar answered Sep 26 '22 22:09

mr. pc_coder