In order to determine if two polygons overlap we need to iterate through the line segments that make up each polygon and determine if any of the line segments intersect. Once we find a single point where the lines intersect we can then stop the iterations. The following code can be used to determine if two polygons overlap:

//poly1 and poly2 are arrays of VELatlongs that represent polygons
function ArePolygonsOverlapped(poly1, poly2)

{

if(poly1.length >= 3 && poly2.length >= 3)

{

//close polygons
poly1.push(poly1[0]);
poly2.push(poly2[0]);
for(var i = 0; i < poly1.length-1;i++)

{

for(var k = 0; k < poly2.length-1; k++)

{

if(SimplePolylineIntersection(poly1[i],poly1[i+1],poly2[k],poly2[k+1])!=null)

return true;

}

}

return false;

}

return null;

}

The code for the function "SimplePolylineIntersection" can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!474.entry

### Like this:

Like Loading...

Dude you go off.. I just all your posts as a massive dump in my google reader. Keep up the great work!

Great work and explanation given

Thanks a lot.

Thanks Ricky, I was wondering if this handles polygon A contains polygon B. Or containing is not consider as overlap?

The algorithm will determine if a polygon intersects (overlaps) or is complete contained. However it won’t just tell you if is complete contains and not overlapping. The algorithm could be easily changed though to determine that.

This is for Google Maps v2 version I guess(which is depreciated and removed). It would be great if you can update for Google Maps v3. If I end up. I will let you know.

Thanks,

Praveen Jegan

Nope. This is Bing Maps v6.3, not Google Maps v2. I don’t use Google Maps, note the title of my blog “Ricky’s Bing Maps Blog”. If you want to use this with the latest version of Bing Maps (v7) you can easily modify this code to work by:

* Give all Latitude and Longitude property names small “L”s.

* Change VELatLong to Microsoft.Maps.Location.