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;
}












November 21st, 2007 at 2:58 am
Hi there folks!
I’ve known about your blog via Arcturus’ blog, and of course I had to get myself dropped by. Looks it’s going to be great! Congratulations, and I look forward to see also the others writing interesting stuff.
This post almost drove me insane. There’s no way to understand this bloody algorithim. I’ve been reading it carefully for twenty minutes, trying to trace it by drawing on a piece of paper,… no way!
So following the link on the article I downloaded the Epolys extension to GPolygon, and I found this funny way of giving references of the source for the algorithim:
Algorithm shamelessly stolen from http://alienryderflex.com/polygon/
lol! don’t miss the explanation, worth reading!!!
Apart from that, is your post about trying to figure out if a point is inside any kind of polygon?, or just related to the circle? Shall this second be the case, wouldn’t it be much easier and faster to check that the euclidian distance between the point and the center of the circle is lesser than the radius?
Take care!
November 23rd, 2007 at 7:50 pm
Hi Pa!
Hope see you tomorrow in the party!
The porpouse of the post is find a algorithm that resolves the problem of the boundingbox with any kind of polygon.
In the next examples we wanna draw a polygon rounding a path, so we need this solution.
Another question, why don“t you enjoy us exploring the gmaps possibilities?
See you soon man!
November 27th, 2007 at 10:37 pm
[…] slight modification to The bounding box problem will let us draw polygons (i.e. circles) centered anywhere on the map with arbitrary radius based […]
January 5th, 2008 at 11:17 am
[…] about prototype to understand this) we have a GMap2 object, an array of points (of interest), an extension on the GPolygon object to determine if a Gpoint is contained in a polygon, and a piece of code we run on body load to put everything together and to add the listeners we are […]
October 2nd, 2008 at 1:32 pm
Hi!
This function realy works!
Small optimization, I’v add following at the beginning of function:
if(!this.getBounds().containsLatLng(point)){
return false;
}
Thanks a lot!