简体   繁体   中英

google maps api polygon to polygon instead of point to polygon collision

There's a method for polygon detection called: containsLocation(point:LatLng, polygon:Polygon)

However, this checks if a point (single co-ordinate) is inside a polygon, but I need it to check if a polygon is inside of another polygon.

Any ideas?

Basically what @geocodezip says. You don't give any clue about how you're creating your polygons, so you maybe don't need to find out all the points like this. But it should work regardless.

I get all the paths of the polygon you want to check. Then loop over those paths, getting all the points and adding them to an array. Then loop over that array, checking if each point falls within the other polygon.

var paths = polygon1.getPaths();
var points = [];

paths.forEach(function(path) {
    path.forEach(function(point) {
        points.push(point);
    });
});

for (var i = 0; i < points.length; i++) {
    if (!google.maps.geometry.poly.containsLocation(points[i], polygon2)) {
        alert('this point is not in the other polygon: ' + points[i].toString());
        break;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM