Android Google Maps PolygonOptions未从给定的坐标集绘制

问题描述 投票:4回答:2

我试图围绕一条路线绘制一个复杂的多边形,遵循给定半径的步骤。为此,我在路线的每个步骤(坐标)周围绘制了50边均匀多边形(实际上是圆形)。现在我获得了路线周围所有绘制圆圈的一组坐标,我可以在地图上看到它们但是它们是重叠的,看起来不是很好看,并且添加如此大量的叠加层并不是一个好习惯。地图。

enter image description here

所以我现在需要做的是将我现在拥有的所有多边形合并到一个多边形中并将其绘制在地图中。

我尝试删除每两个多边形的交点(测试polygon1的点是否位于polygon2内,反之亦然)并合并一个数组中的所有其余坐标,然后构造我的新多边形,但它不起作用。这是我如何做到这一点的代码片段:

public ArrayList<PolygonOptions> polygons = new ArrayList<>();

// lineOptions is the set of route coordinates
    for (int i = 0; i < lineOptions.getPoints().size() - 1; i++) {
        // Draw a circle around each point of the route
        PolygonOptions circle1 = drawCircle(lineOptions.getPoints().get(i), 0.1);
        PolygonOptions circle2 = drawCircle(lineOptions.getPoints().get(i + 1), 0.1);
        // Draw a convex hull between every two successive circles
        PolygonOptions convexHull = convexHull(circle1, circle2);

        convexHull.strokeWidth(0);
        convexHull.fillColor(0x7F729E47);
        activity.range.add(activity.mMap.addPolygon(convexHull));
        polygons.add(convexHull);
    }


if (polygons.size() == 1) {
        pts.addAll(polygons.get(0).getPoints());
    } else {
        for (int i = 0; i < polygons.size() - 1; i++) {
            ArrayList<LatLng> pts1 = new ArrayList<>();
            ArrayList<LatLng> pts2 = new ArrayList<>();
            pts1.addAll(polygons.get(i).getPoints());
            pts2.addAll(polygons.get(i + 1).getPoints());

            for (int j = 0; j < pts1.size(); j++) {
                if (pointInPolygon(pts1.get(j), pts2)) {
                    pts1.remove(j);
                }
            }

            for (int j = 0; j < pts2.size(); j++) {
                if (pointInPolygon(pts2.get(j), pts1)) {
                    pts2.remove(j);
                }
            }

            pts.addAll(pts1);
            pts.addAll(pts2);
        }
    }

// This part didn't work
// PolygonOptions range = new PolygonOptions();
// range.addAll(pts);
// range.strokeWidth(0);
// range.fillColor(0x7F729E47);
// activity.range.add(activity.mMap.addPolygon(range));
android google-maps convex-hull convex-polygon
2个回答
1
投票

您需要计算线的缓冲区。据Wikipedia说:

缓冲区是由边界区域定义的区域,该边界区域由沿着对象的片段的所有节点的指定最大距离处的一组点确定。

使用Minkowski sum计算几何的缓冲区。 Minkowsky总和需要两个多边形(一个是你的折线,另一个是圆形,如果你想要圆形的帽子)并将第二个移动到第一个的路径上。

您可以找到可以学习的Minkowski总和的一些具体实现。例如https://github.com/perelo/computational-geometry/blob/master/src/computational_geometry/model/algorithms/Minkowski.java


2
投票

按照@ antonio的指示,并使用JTS Topology Suit,我能够在路线周围绘制一个多边形(缓冲路线)。当我在JTS库中使用缓冲区函数时,我获得了路径的缓冲区,但是盖子是椭圆形的,我读到它并且发生这种情况是因为计算出的坐标投影在地球的地图上而不是平坦的,帽子变得更加椭圆形当路线更接近地球的极点时,更接近赤道线(纬度0度)时更圆。无论如何,我使用了库中的另一个功能来结合我在问题中已经拥有的所有多边形,这就是结果:

enter image description here

    public ArrayList<Polygon> polys = new ArrayList<>();

    //lineOptions is the set of route coordinates
    for (int i = 0; i < lineOptions.getPoints().size() - 1; i++) {
        // Draw a circle around each point of the route
        PolygonOptions circle1 = drawCircle(lineOptions.getPoints().get(i), 0.1);
        PolygonOptions circle2 = drawCircle(lineOptions.getPoints().get(i + 1), 0.1);


        // Draw a convex hull between every two successive circles
        PolygonOptions convexHull = convexHull(circle1, circle2);



        List<LatLng> latLngs = convexHull.getPoints();
        List<Coordinate> coords = new ArrayList<>();

        for (int j=0; j<latLngs.size(); j++) {
            coords.add(new Coordinate(latLngs.get(j).latitude, latLngs.get(j).longitude));
            if(j==latLngs.size()-1)
                coords.add(new Coordinate(latLngs.get(0).latitude, latLngs.get(0).longitude));
        }

        Coordinate[] coordinates = coords.toArray(new Coordinate[coords.size()]);

        GeometryFactory fact = new GeometryFactory();
        LinearRing linear = new GeometryFactory().createLinearRing(coordinates);
        Polygon poly = new Polygon(linear, null, fact);

        polys.add(poly);

    }


    PolygonOptions combine = combineIntoOnePolygon(polys);
    combine.strokeWidth(0);
    combine.fillColor(0x7F729E47);
    activity.range = activity.mMap.addPolygon(combine);

执行组合的功能:

static PolygonOptions combineIntoOnePolygon(Collection<Polygon> geometries ){
    Geometry all = null;
    PolygonOptions allPolygon = new PolygonOptions();

    for(Iterator<Polygon> i = geometries.iterator(); i.hasNext(); ){
        Polygon geometry = i.next();
        if( geometry == null ) continue;
        if( all == null ){
            all = geometry;
        }
        else {
            all = all.union( geometry );
        }
    }

    List<Coordinate> bufferCoordinates = Arrays.asList(all.getCoordinates());

    for (Coordinate c : bufferCoordinates) {
        allPolygon.add(new LatLng(c.x, c.y));
    }

    return allPolygon;
}
© www.soinside.com 2019 - 2024. All rights reserved.