# Polygons with holes (complex polygons)

• ### Question

• We have followed the instructions found in an example of how to create polygons with holes and it is not currently working for us. Is this functionality not included in the free Bing Maps API version? Do we need to be a paying customer for this to work? Thank you in advance.
Monday, June 10, 2019 3:59 PM

### All replies

• Hi Joshua,

Please take a look at the following sample.  It has two polygons, with holes, and shows how you can perform binary math with them as well.

https://www.bing.com/api/maps/sdkrelease/mapcontrol/isdk/binaryoperations

My suspicion is that you have a definition issue with your polygon as the sample runs each time the page is launched and is working today.

Sincerely,

IoTGirl

Monday, June 10, 2019 4:04 PM
• Can you provide a code block showing one of your polygons. Creating a polygon with a hole simply requires an array or array of coordinates where an array of coordinates represents a ring of a polygon. The first ring should be the outer outline and all additional rings should be overtop of the first ring and would be used to cut out holes. The first and last coordinates of a ring should be the same as well, but I believe there is logic in the polygon class that automatically closes rings if you haven't. This is an Open Geospatial Consortium standard (OGC).
Monday, June 10, 2019 4:39 PM
• function buildBingShape(polygon, layer, fill, stroke, insidePoly) {
var polygonholder = buildPoly(polygon);
var insidePolygonHolder = buildPoly(insidePoly);
if (polygonholder.length > 0 && insidePolygonHolder.length > 0) {
var polyArray = [ polygonholder, insidePolygonHolder ];
var poly = new Microsoft.Maps.Polygon(polyArray, {
fillColor : fill,
strokeColor : stroke,
strokeThickness: 2
});
map.entities.push(poly);
return poly;
} else if (polygonholder.length > 0) {
var poly = new Microsoft.Maps.Polygon(polygonholder, {
fillColor : fill,
strokeColor : stroke,
strokeThickness: 2
});
map.entities.push(poly);
return poly;

}
}
function buildPoly(polygon) {
var polygonholder = new Array();
if (polygon != undefined && polygon != null) {
for (var i = 0; i < polygon.length; i++) {
polygonholder[i] = new Microsoft.Maps.Location(polygon[i][1],
polygon[i][0]);
}
}
return polygonholder;
}
Tuesday, June 11, 2019 6:38 PM
• Hi Joshua,

Can you please provide your actual polygon definition?  To confirm Ricky's statements:

1. An array of coordinates where an array of coordinates represents the outer ring of a polygon.
2. All additional rings should be inside of the first ring and would be used to cut out holes.
3. The first and last coordinates of any ring should be the same as well.

Sincerely,

IoTGirl

Tuesday, June 11, 2019 7:01 PM
• We are using polygon features from GIS data, so the polygons are made up of points coming from a database. We are not manually entering the polygon definitions. Right now the application is drawing both polygons with the smaller one on top of the first one instead of cutting out the second shape.
Tuesday, June 11, 2019 7:39 PM
• Hi Joshua,

That would indicate a problem with your GIS data.  For example here is a definition of South Africa with Lesotho cut out.  https://www.bing.com/api/maps/sdkrelease/mapcontrol/isdk/geojsonreadobject

Note that this is GeoJSON format, a recognized standard for GIS data.

Sincerely,

IoTGirl

Tuesday, June 11, 2019 8:54 PM
• A couple of things to verify:

What format is the polygon data in when sent to the browser? I noticed that in the buildPoly function you have "polygon[i][1], polygon[i][0]". Want to make sure you have latitude/longitude, which Bing Maps requires, as most GIS systems use longitude/latitude, so easy thing to cause issues.

I see you are using "map.entities". That is a very old legacy way of adding data to the map. This has limited functionality and is only there for older apps that were pushed to version 8 from version 7. Use layers instead, they have a lot more functionality and better supported.

In the buildBingShape function you add the shape to the map and also return the shape. Are you adding the shape again elsewhere? If so, this might cause issues.

Tuesday, June 11, 2019 11:40 PM