java 如何找到多个点的中心?安卓
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5747553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find the center of multiple points? Android
提问by digitalWestie
I have multiple points placed on my MapView and would like to centre the map on these points i.e. not any particular one, just the averaged centre of the lot of them.
我在我的 MapView 上放置了多个点,并希望将地图集中在这些点上,即不是任何特定的点,而是其中许多点的平均中心。
I know there's a method for this in the Javascript G. Maps API. I was wondering whether there was a prexisting method to do this in Android?
我知道在 Javascript G. Maps API 中有一个方法。我想知道在 Android 中是否有一种预先存在的方法可以做到这一点?
采纳答案by Someone Somewhere
this question looks suspiciously like one I had some time ago ...
这个问题看起来很像我前段时间问过的问题……
Android MapView -setting zoom automatically until all ItemizedOverlay's are visible
回答by Asaf
What you are trying to calculate is called a Centroid. There are several Java implementations for calculating centroids for a finite set of points (a polygon).
您要计算的内容称为Centroid。有几种 Java 实现可用于计算有限点集(多边形)的质心。
For example: JavaGeomhas an implementationfor finding the centroid of multiple 2D points.
I'm not sure if there's a standard android implementation for it, but it is fairly simple to implement yourself.
例如:JavaGeom有执行查找的多的2D点的重心。
我不确定它是否有标准的 android 实现,但是自己实现是相当简单的。
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;
}
回答by JoxTraex
You could take the average of the points x and y respectively, and then set the span based on these values and introduce a padding like 25% more than the value to insure that there is enough space to focus on it.
您可以分别取 x 和 y 点的平均值,然后根据这些值设置跨度,并引入比值多 25% 的填充,以确保有足够的空间来关注它。
for more info check this out: http://code.google.com/android/add-ons/google-apis/reference/index.html
有关更多信息,请查看:http: //code.google.com/android/add-ons/google-apis/reference/index.html
回答by Zanna
I would not use the centroid (or barycenter, or centre of mass in case of uniform density), if you need to show a figure (a shapre, an area) in a layout. What I really need to center a figure in a layout, is the maximum and minimum of all the coordinates to show every 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);
}
回答by Dapaintballer331
Don't take the average of points x and y.. Google "centroid psuedocode" and that should be what you want.
不要取 x 和 y 点的平均值 .. 谷歌“质心伪代码”,这应该是你想要的。