How to find the center of several points? Android

I have several points located on my MapView, and I would like to center the map on these points, that is, not any specific, just an average center of them.

I know that there is a method for this in the Javascript G. Maps API. I was wondering if there is a preliminary method for this in Android?

+3
java android android-mapview
source share
5 answers

this question looks suspiciously like the one I had some time ago ...

Android MapView - auto-scale until all ItemizedOverlay is visible

+2
source share

What you are trying to calculate is called Centroid . There are several Java implementations for calculating centroids for a finite set of points (polygon).

For example: JavaGeom has an implementation for searching centroids of several two-dimensional points.
I'm not sure if there is a standard android implementation for it, but it is quite simple to implement it.

Simple implementation:

public static float[] centroid(float[] points) { float[] centroid = { 0, 0 }; for (int i = 0; i < points.length; i+=2) { centroid[0] += points[i]; centroid[1] += points[i+1]; } int totalPoints = points.length/2; centroid[0] = centroid[0] / totalPoints; centroid[1] = centroid[1] / totalPoints; return centroid; } 
+15
source share

You can take the average value of the x and y points, respectively, and then set the range based on these values ​​and introduce an addition that is 25% larger than the value to provide enough space to focus on it.

for more information check this out: http://code.google.com/android/add-ons/google-apis/reference/index.html

+1
source share

I would not use a centroid (or a barycenter, or a center of mass in the case of a uniform density) if you need to show a figure (figure, region) in the layout. What I really need to center the figure in the layout is the maximum and minimum of all coordinates to show each point.

 ArrayList<GeoPoint> track; public GeoPoint CenterMap() { double longitude = 0; double latitude = 0; double maxlat = 0, minlat = 0, maxlon = 0, minlon = 0; int i = 0; for (GeoPoint p : track) { latitude = p.getLatitude(); longitude = p.getLongitude(); if (i == 0) { maxlat = latitude; minlat = latitude; maxlon = longitude; minlon = longitude; } else { if (maxlat < latitude) maxlat = latitude; if (minlat > latitude) minlat = latitude; if (maxlon < longitude) maxlon = longitude; if (minlon > longitude) minlon = longitude; } i++; } latitude = (maxlat + minlat) / 2; longitude = (maxlon + minlon) / 2; return new GeoPoint(latitude, longitude); } 
+1
source share

Don't take the average of the x and y points .. Google's "Centroid psuedocode" and that should be what you want.

-one
source share

All Articles