Anyone can help me get center point. I have 6 LatLng objects, now i need to get a LatLng object is center. Many thanks!
-
1refer http://stackoverflow.com/questions/4656802/midpoint-between-two-latitude-and-longitude – sasikumar Feb 26 '16 at 07:00
-
Thanks sir, but that's just 2 points, i have 6 points or more. – Na Pro Feb 26 '16 at 07:12
9 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);
}

- 18,044
- 4
- 45
- 61
-
3Incorrect answer! This will fail on points around longitude -180/180 degrees. – zyamys Nov 08 '17 at 18:23
-
1@zyamys you may be right, feel free to edit the answer if you want to improve it :) – antonio Nov 08 '17 at 18:51
-
-
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;
}
return LatLng(latitude / n, longitude / n);
}

- 2,366
- 1
- 21
- 26
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

- 16,412
- 3
- 32
- 54
-
thanks for your comment, but there is just 2 points, i have 6 points or more. – Na Pro Feb 26 '16 at 07:03
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

- 1
- 1

- 16,412
- 3
- 32
- 54
-
-
yes js but important one is what u need ,u need LatLngBounds to do it ,also if u want to zoom depends on the 6 points so u need center i also offer u to look at http://www.androidhive.info/2012/08/android-working-with-google-places-and-maps-tutorial/ this link, it will be helpful – mr. pc_coder Feb 26 '16 at 07:23
-
Thanks for your suggestion, i have tried getting mid-lat as (smallest-lat + greatest-lat) / 2, the same for longitude but i feel that's not correct. Maybe i will use this way until have other solutions. – Na Pro Feb 26 '16 at 07:29
-
http://stackoverflow.com/questions/5747553/how-do-i-find-the-center-of-multiple-points-android?lq=1 look at also this u should use centroid also – mr. pc_coder Feb 26 '16 at 07:37
You can get midpoint as below -
double lat1, lng1, lat2, lng2;
double midlat = (lat1 + lat2)/2;
double midlng = (lng1 + lng2)/2;

- 2,727
- 14
- 39
-
Thanks Kevz, but here is just 2 points, i have 6 points, i have tried getting mid-lat as (smallest-lat + largest-lat) / 2, the same for longitude but i feel that's not correct. – Na Pro Feb 26 '16 at 07:07
-
1No! Does not take the international dateline (-180/180 longitude) into account. – zyamys Nov 08 '17 at 18:24
This consists of two main steps
- Step 1: Simplify all LatLngs to only two extreme LatLngs
- Step 2: Get the center of those two computed LatLngs
1) Simplifying
We want to to get the least and most latitudes of all coordinates given, not choose the most extreme/furthest coordinate out of them. This can be achieved by:
List<Object> getMapFittingDataWithLatLongs(List<LatLng> latLongs) {
List<LatLng> extremeLatLongs = getExtremeLatLongs(latLongs);
LatLng latLng1 = extremeLatLongs[0], latLng2 = extremeLatLongs[1];
double extremeDistance = Geolocator.distanceBetween(
latLng1.latitude,
latLng1.longitude,
latLng2.latitude,
latLng2.longitude,
);
LatLng centeredLatLong = getCenterLatLong(latLng1, latLng2);
return [centeredLatLong, 12.0];
}
2) Centering
We will deal with Latitude and Longitude similary, but before hand we need to know that their limits are 180
and -180
, and both limits physically touch. So we need to be careful and don't just get the average of both values (as (180+ -180)/2 = 0
which is the total opposite side of the Globe!!)
Averaging numbers won't be a problem though when the distance value difference is <= 90
and the safest part to do the average is between -90
and 90
in other words, in the 2nd and 3rd quartiles.
However in the 1st and 4th quartiles we need to be a little careful. This depends on knowing where will the final result be in the 1st or 4th quartile based on both lats/lngs.
If the result is in the 1st quartile then we get the absolute average of both and subtract the outcome from 180
, to compensate the radial shift after averaging. But if the output is known to be in the 4th quartile then we add it to -180
as shown in the snippet below:
LatLng getCenterLatLong(LatLng latLng1, LatLng latLng2) {
double midLat, midLong;
// Calculating midLat
int coefficient = 1;
if ((latLng1.latitude > 90 && latLng2.latitude < -90) || (latLng2.latitude > 90 && latLng1.latitude < -90)) {
midLat = 180 - ((latLng1.latitude - latLng2.latitude) / 2).abs();
// If output will be in the 1st quartile then co-ef will remain 1, if in 4th then it will be -1
if ((latLng1.latitude < 0 && latLng1.latitude.abs() < latLng2.latitude.abs()) ||
(latLng2.latitude < 0 && latLng2.latitude.abs() < latLng1.latitude.abs())) coefficient = -1;
// Applying coefficient
midLat *= coefficient;
} else
midLat = (latLng2.latitude + latLng1.latitude) / 2;
// Calculating midLong
coefficient = 1;
if ((latLng1.longitude > 90 && latLng2.longitude < -90) || (latLng2.longitude > 90 && latLng1.longitude < -90)) {
midLong = 180 - ((latLng1.longitude.abs() - latLng2.longitude.abs()) / 2).abs();
// If output will be in the 1st quartile then co-ef will remain 1, if in 4th then it will be -1
if ((latLng1.longitude < 0 && latLng1.longitude.abs() < latLng2.longitude.abs()) ||
(latLng2.longitude < 0 && latLng2.longitude.abs() < latLng1.longitude.abs())) coefficient = -1;
// Applying coefficient
midLong *= coefficient;
} else
midLong = (latLng2.longitude + latLng1.longitude) / 2;
return LatLng(midLat, midLong);
}
Good luck.

- 441
- 3
- 11
If you're searching for a Dart code to use in Flutter to center a polygon in the map use the code below.
static LatLng getCentroid(List<LatLng> points) {
if(points.isEmpty) {
return const LatLng(0, 0);
} else if (points.length == 1) {
return points[0];
}
double lowerX = points[0].latitude;
double lowerY = points[0].longitude;
double higherX = points[0].latitude;
double higherY = points[0].longitude;
for (int i = 1; i < points.length; i++) {
if(points[i].latitude > higherX) {
higherX = points[i].latitude;
}
if(points[i].latitude < lowerX) {
lowerX = points[i].latitude;
}
if(points[i].longitude > higherY) {
higherY = points[i].longitude;
}
if(points[i].longitude < lowerY) {
lowerY = points[i].longitude;
}
}
return LatLng((higherX + lowerX) / 2, (higherY + lowerY) / 2);
}

- 56
- 1
- 5
For less Stress -
i prefer picking one from the markers and use it as the center,
but by doing this, make sure that markers have valid lat and lng, and also center of the has no big meaning in your app.

- 278
- 4
- 11
Sum all latitude and / numbers of latitude Or longitude Like :
double CenterLat = (lat1 + lat2 + lat3 + lat4 + lat5 + lat6) / 6;
double CenterLon = (lon1 + lon2 + lon3 + lon4 + lon5 + lon6) / 6;
LatLng Center = new LatLng(CenterLat, CenterLon);

- 1,384
- 2
- 17
- 29

- 17
- 1
- 7
-
No! Does not take the international dateline (-180/180 longitude) into account. – zyamys Nov 08 '17 at 18:25
-
-
-
@DranoMax got it. This won't work with points like `(0;-179)` and `(0;179)`. We get `(0;0)`, when it should be `(0;180)` – opewix Mar 13 '20 at 11:36