How do I calculate the center of a polygon in Google Maps Android API v2?
Below code I am using to find the center point of the polygon. its working for me too
private LatLng getPolygonCenterPoint(ArrayList<LatLng> polygonPointsList){
LatLng centerLatLng = null;
Builder builder = new LatLngBounds.Builder();
for(int i = 0 ; i < polygonPointsList.size() ; i++)
{
builder.include(polygonPointsList.get(i));
}
LatLngBounds bounds = builder.build();
centerLatLng = bounds.getCenter();
return centerLatLng;
}
Below is the code which I am using now to find the center of polygon:-
public static double[] centroid(List<PolyPoints> points) {
double[] centroid = { 0.0, 0.0 };
for (int i = 0; i < points.size(); i++) {
centroid[0] += points.get(i).getLatitude();
centroid[1] += points.get(i).getLongitude();
}
int totalPoints = points.size();
centroid[0] = centroid[0] / totalPoints;
centroid[1] = centroid[1] / totalPoints;
return centroid;
}
private static LatLng getCenterOfPolygon(List<LatLng> latLngList) {
double[] centroid = {0.0, 0.0};
for (int i = 0; i < latLngList.size(); i++) {
centroid[0] += latLngList.get(i).latitude;
centroid[1] += latLngList.get(i).longitude;
}
int totalPoints = latLngList.size();
return new LatLng(centroid[0] / totalPoints, centroid[1] / totalPoints);
}