Baby
Baby

Reputation: 326

How to draw polygons with hole(s) in WKT geometry

I am developing an application that uses a map. I want to show a polygon with a "hole", in Java Android. I searched, but unfortunately, I could not find a solution. I think my problem is that I can not set the correct fillColor. Can someone help me?

My result:

enter image description here

I want the hole's color to be transparent.

My code:

List<ArrayList<LatLng>> multiLatLon;
...
//draw polygon hole 
for(int i=0; i<multiLatLon.size(); i++){
                    poly = new PolygonOptions();
                    for (int j=0; j<multiLatLon.get(i).size(); j++){
                        mop.position(multiLatLon.get(i).get(j));
                        poly.add(multiLatLon.get(i).get(j));
                        Marker m = mMap.addMarker(mop);
                    }
                    poly.fillColor(R.color.colorOcher);
                    Polygon polygon = mMap.addPolygon(poly);
                }

Let me know if you need more info.

Solution:

...
poly = new PolygonOptions ();
poly.fillColor (ColorUtils.setAlphaComponent (Color.BLUE, 128));
for (int i = 0; i <multiLatLon.size (); i ++) {
 if (i == 0) {
  poly.addAll (multiLatLon.get (i));
 } else {
  poly.addHole (multiLatLon.get (i));
 }
}
mMap.addPolygon(poly);

In my case, I know that the first point array (multiLatLon.get (i)) defines the polygon geometry; while the others are the polygon holes.

Note: I used addAll to delete one for loop

Upvotes: 1

Views: 700

Answers (1)

Sirmyself
Sirmyself

Reputation: 1564

I think the solution you're looking for is the addHole function in the PolygonOptions class.

Give that function your points (as Iterable<LatLng>) you want to have a hole and you should be good to go.

I don't know exactly where the values of your hole are in your code, but basically, you just call that function like this :

poly = new PolygonOptions();
// set the polygon's attributes
//...
//Iterable<LatLng> hole = //whatever contains the hole
poly.addHole(hole);

Upvotes: 3

Related Questions