Reputation: 735
Anyone can help me get center point. I have 6 LatLng objects, now i need to get a LatLng object is center. Many thanks!
Upvotes: 7
Views: 17514
Reputation: 345
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.
Upvotes: 0
Reputation: 66
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);
}
Upvotes: 1
Reputation: 451
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];
}
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.
Upvotes: 0
Reputation: 2616
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);
}
Upvotes: 5
Reputation: 15
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);
Upvotes: -3
Reputation: 18262
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);
}
Upvotes: 12
Reputation: 17610
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
Upvotes: 0
Reputation: 17610
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
Upvotes: 3
Reputation: 2737
You can get midpoint as below -
double lat1, lng1, lat2, lng2;
double midlat = (lat1 + lat2)/2;
double midlng = (lng1 + lng2)/2;
Upvotes: 0