如何找到多个点的中心?Android的

dig*_*tie 3 java android android-mapview

我有多个点放在我的MapView上,并希望将地图置于这些点上,即不是任何特定的点,只是它们的平均中心.

我知道在Javascript G. Maps API中有一种方法.我想知道在Android中是否有一种先行方法可以做到这一点?

Asa*_*saf 15

您要计算的内容称为Centroid.有几种Java实现用于计算有限点集(多边形)的质心.

例如:JavaGeom执行查找的多的2D点的重心.
我不确定它是否有标准的Android实现,但是实现起来相当简单.

简单实施:

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;
}
Run Code Online (Sandbox Code Playgroud)


Som*_*ere 2

这个问题看起来很像我前一段时间的问题......

Android MapView - 设置自动缩放,直到所有 ItemizedOverlay 都可见