Skip to content Skip to sidebar Skip to footer

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!

Solution 1:

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

    returnnewLatLng(latitude/n, longitude/n);
}

Solution 2:

publicstaticvoidmidPoint(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

Solution 3:

Dart version of Java code above

LatLng computeCentroid(Iterable<LatLng> points){
  double latitude = 0;
  double longitude = 0;
  int n = points.length;

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

  returnLatLng(latitude / n, longitude / n);
}

Solution 4:

var bound = new google.maps.LatLngBounds();

for (i = 0; i < locations.length; i++) {
  bound.extend( new google.maps.LatLng(locations[i][2], locations[i][3]) );

  // OTHER CODE
}

console.log( bound.getCenter() );

u write your locations in array called locations then in loop do it Find center of multiple locations in Google Maps this is js code so u can change it to your code

Solution 5:

You can get midpoint as below -

double lat1, lng1, lat2, lng2;

double midlat = (lat1 + lat2)/2;
double midlng = (lng1 + lng2)/2;

Post a Comment for "How To Get Center Point(latlng) Between Some Locations?"