Center of a cluster of latitude and longitude points in Google map android -


i trying center of shape in android. shape draw hand on map. have of co-ordinates mean values turning out trickier thought. want plot marker on mean latitude , longitude.

i have tried summing latitude , longitude separately , dividing number of points. not give correct answer. marker seems trailing behind draw figure. have tried using implementation gives same answer, previous question, calculate center point of multiple latitude/longitude coordinate pairs

the code have been using:

private void calculatefreehandpolygonparameters(){      double xvalues = 0;     double yvalues = 0;     double zvalues = 0;     int count = 0;      // linesforpolygon list of lines in polygon     for(polyline line : linesforpolygon){         (latlng point : line.getpoints()) {             xvalues += math.cos(math.toradians(point.latitude)) * math.cos(math.toradians(point.longitude));             yvalues += math.cos(math.toradians(point.latitude)) * math.sin(math.toradians(point.longitude));             zvalues += math.sin(math.toradians(point.latitude));             count++;         }     }      double meanx = xvalues/count;     double meany = yvalues/count;     double meanz = zvalues/count;      double centrallongitude = math.atan2(meany, meanx);     double centralsquareroot = math.sqrt(math.pow(meanx, 2) + math.pow(meanx, 2) + math.pow(meanx, 2));     double centrallatitude = math.atan2(meanz, centralsquareroot);      double latitude = math.todegrees(centrallatitude);     double longitude = math.todegrees(centrallongitude);      log.i("maps", "freehand parameters: x mean -> " + latitude + " y mean -> " + longitude);      testmarker = mmap.addmarker(new markeroptions()             .position(new latlng(latitude, longitude))             .title("polygon center")             .snippet("lat: " + latitude + " long: " + longitude)); } 

here centroid code:

public class polygoncentroid {      private list<geopoint> points;     private int pointssize;      public polygoncentroid(list<geopoint> points) {         this.points = points;         this.pointssize = points.size();     }      protected double polygonarea() {         double area = 0;         (int = 0, j; < pointssize; i++) {             j = (i + 1) % pointssize;             area += points.get(i).getlongitude() * points.get(j).getlatitude();             area -= points.get(i).getlatitude() * points.get(j).getlongitude();         }         area /= 2.0;         return area;     }      public geopoint centroid() {         double cx = 0, cy = 0;         double factor;         (int = 0, j; < pointssize; i++) {             j = (i + 1) % pointssize;             factor = (points.get(i).getlongitude() * points.get(j).getlatitude() - points.get(j).getlongitude() * points.get(i).getlatitude());             cx += (points.get(i).getlongitude() + points.get(j).getlongitude()) * factor;             cy += (points.get(i).getlatitude() + points.get(j).getlatitude()) * factor;         }         double = polygonarea();         factor = 1.0 / (6.0 * a);         cx *= factor;         cy *= factor;         return new geopoint(cy, cx);     }  } 

enter image description here

also, please pay attention centroid can outside of polygon:

enter image description here

full source code , usage.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -