谷歌地图android

时间:2016-04-21 20:57:29

标签: android google-maps coordinates

我想在android中获得一个形状的中心。在地图上手工绘制形状。我有所有的坐标,但平均值比我想象的要复杂。我想在平均纬度和经度上绘制一个标记。

我已经尝试分别总结纬度和经度,然后除以点数。这没有给出正确的答案。标记似乎总是落后于绘图。我也尝试过使用该实现,但它给出了相同的答案,前一个SO问题,Calculate the center point of multiple latitude/longitude coordinate pairs

我一直在使用的代码:

private void calculateFreeHandPolygonParameters(){

    double xValues = 0;
    double yValues = 0;
    double zValues = 0;
    int count = 0;

    // linesForPolygon a list of the lines in the polygon
    for(Polyline line : linesForPolygon){
        for (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));
}

2 个答案:

答案 0 :(得分:1)

  1. 您实际上需要计算所谓的CENTROID。这不是一件小事,请看这里: http://www.spatialanalysisonline.com/HTML/index.html?centroids_and_centers.htm
  2. 注意所谓的GREAT CIRCLE,在飞机地图上召回飞行计划......因此,如果距离相对较大,则需要牢记这一现象。
  3. 这是一个经典的GIS问题,在您了解算法的伪代码后,它是简单的编码任务...试试gis.stackexchange.com

答案 1 :(得分:1)

这是我的质心代码:

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;
        for (int i = 0, j; i < 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;
        for (int i = 0, j; i < 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 A = polygonArea();
        factor = 1.0 / (6.0 * A);
        cx *= factor;
        cy *= factor;
        return new GeoPoint(cy, cx);
    }

}

enter image description here

另外,请注意质心可以在多边形之外:

enter image description here

Full source codeusage