Nov
19
The boundingbox problem
Posted by arcturus
Again with google maps, now we are going to talk about the bounding box problem.
The google api provides the function containsBounds:
| containsPoint(point) | Returns true if the rectangular area (inclusively) contains the pixel coordinates. (Since 2.88) |
The problem is very simple, we wanna check points of interest inside the polygon, not the rectangular area that includes it.

We have solved this problem learning how to extend the GPolygon class with a method Contains that does what we want. You can see more examples of extending this class from this link.
Here is the code we used:
GPolygon.prototype.contains = function(point) { var j=0;
var oddNodes = false;
var x = point.lng();
var y = point.lat();
for (var i=0; i < this.getVertexCount(); i++) {
j++;
if (j == this.getVertexCount()) {j = 0;} if (((this.getVertex(i).lat() < y)
&& (this.getVertex(j).lat() >= y))
|| ((this.getVertex(j).lat() < y) && (this.getVertex(i).lat() >= y))) {
if ( this.getVertex(i).lng() + (y - this.getVertex(i).lat())
/ (this.getVertex(j).lat()-this.getVertex(i).lat())
* (this.getVertex(j).lng() - this.getVertex(i).lng())>x){>
oddNodes = !oddNodes;
}
}
}
return oddNodes;
}











