Determining if two polygons are overlapping in Google Maps?

I'm working with google maps and polygons, but I have to validate that polygons are not overlapping each other.

I have a function that returns TRUE when a point (lat, lng) is inside a polygon, but it's not enough to determine if every point of a polygon is inside another polygon.

Any suggestion?

• Took me about 10 seconds to find github.com/tparkin/Google-Maps-Point-in-Polygon. Did you try anything? Commented Mar 18, 2016 at 12:15
• He wrote "I have a function that returns TRUE when a point (lat, lng) is inside a polygon". I think it is a good trial but does not answer if the whole polygon is inside another. Commented Mar 18, 2016 at 12:37
• I would add the Javascript Topology Suite library to your project and take advantage of its spatial operators for this sort of comparison. I provided an answer on a similar question showing how to include the jsts library in a project and perform a dissolve/union on two polygon geometries. You might find that helpful. If I have a chance I'll see if I can find or create an example that tests for intersection of two geometries--polygon, line, or point will not matter, they either intersect or they don't. :) Commented Mar 18, 2016 at 15:15
• Have a look at the intersect method of the LatLngBounds class Commented Apr 6, 2020 at 13:02

JSFiddle Example

I've created a JSFiddle demonstrating a solution to your problem using the JavaScript Topology Suite (JSTS) (JSTS) library.

Explaination

There are two steps to this approach. The first step converts your Google geometries into WellKnownText (WKT) geometry expressions, which is a widely supported format. The second step uses JSTS to perform a JSTS `geometry.intersects()` comparison of two WKT geometries.

To really understand this you'll need to have a basic understanding of WKT. Because the polygon geometries in your Google Map are not widely supported format, I immediately convert them into WKT geometries so that we can work with them in JSTS.

To do this easily, I used the Wicket library. Of course you can always home-roll your own Google-Polygon-to-WKT method, or you're welcome to use one I wrote once upon a time, or you can use some other solution you might find. Personally, these days I just use Wicket, which as you can see, is wicked-simple:

``````// Pass in two Google Polygon objects.
// It returns two WellKnownText (WKT) geometry expressions.
//
{
var wicket = new Wkt.Wkt();

wicket.fromObject(poly1);
var wkt1 = wicket.write();

wicket.fromObject(poly2);
var wkt2 = wicket.write();

return [wkt1, wkt2];
}
``````

Next is the meat and potatoes--using JSTS to take two WKT geometries and test whether or not they intersect. Once again, relying on the library, there is not much to it:

``````// Pass in two WKT geometry expressions.
// It performs a JSTS intersects() comparison.
//
function UseJstsToTestForIntersection( wkt1, wkt2 )
{
// Instantiate JSTS WKTReader and get two JSTS geometry objects

if (geom2.intersects(geom1)) {
} else {
``````http://arthur-e.github.io/Wicket/wicket.js