user1953131
user1953131

Reputation: 3

Google Maps Android API v2 - Hollow Polygon not correctly drawn

The code below doesn't work with Google Maps API v2. The polygons (outer and inner polygons) are drawn with the right border, but the fill color of the outer one is not drawn.

PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.add(outerCoordinates);
polygonOptions.addHole(Arrays.asList(innerCoordinates));
polygonOptions.fillColor(Color.BLUE);
polygonOptions.strokeWidth(1.0f);

Does anybody face the same problem?

Upvotes: 0

Views: 1919

Answers (2)

Brais Gabin
Brais Gabin

Reputation: 5935

The vertices must be added in counterclockwise order. Reference

I wrote a function to determinate if a List<LatLng> is clockwise. The code is an implementation of this answer:

public boolean isClockwise(List<LatLng> region) {
    final int size = region.size();
    LatLng a = region.get(size - 1);
    double aux = 0;
    for (int i = 0; i < size; i++) {
        LatLng b = region.get(i);
        aux += (b.latitude - a.latitude) * (b.longitude + a.longitude);
        a = b;
    }
    return aux <= 0;
}

Before adding the polygon points put these three lines:

if (isClockwise(polygon)) {
    Collections.reverse(polygon);
}

Upvotes: 0

AlexWien
AlexWien

Reputation: 28767

Check whether there is a requirement that polygon coordinates have to be clockwise (or counterclockwise) ordered. Try to change the order.

Upvotes: 2

Related Questions