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

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

0
android google-maps
source share
5 answers

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

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

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); } 
+2
source share
 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

+1
source share
 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 an array called locations, then do it in a loop Find the center of several locations on Google Maps is a js code, so you can change it to your code

+1
source share

You can get the midpoint as below -

 double lat1, lng1, lat2, lng2; double midlat = (lat1 + lat2)/2; double midlng = (lng1 + lng2)/2; 
0
source share

Summarize all latitude and / number of latitude or longitude. How:

 double CenterLat = (lat1 + lat2 + lat3 + lat4 + lat5 + lat6) / 6; double CenterLon = (lon1 + lon2 + lon3 + lon4 + lon5 + lon6) / 6; LatLng Center = new LatLng(CenterLat, CenterLon); 
-2
source share

All Articles