Detecting if polygon is in current bounds

576 views
Skip to first unread message

CroNiX

unread,
Nov 15, 2010, 8:39:52 PM11/15/10
to Google Maps JavaScript API v3
Is there a more efficient way to detect if any part of a polygon is in
the current map bounds than to cycle through the polygons' points and
manually test each point or is there a better way? I did not see a
native way to test a polygon like you can with a point using
contains(). The docs say contains() requires a single latLng object
as the parameter. Its too bad it can't test whether the parameter is
an array and do the same as below if it was, at least it doesn't seem
so from the docs....

I am currently doing this for my polygon detection, which works very
well but looks like it can eat some horsepower if used on complex
polygons, especially if tied to the bounds_changed event.

function isPolyInBounds(map, polygon) { //map obj, polygon obj
var currentBounds = map.getBounds();
var polyPoints = polygon.getPath(); //get path of current polygon
var inBounds = false; //this will be set to true if at least 1
point is in the current bounds

//using jquery to iterate over the points and test whether each
point is in current bounds
$(polyPoints.b).each(function(i, point){
if(currentBounds.contains(point)) {
inBounds = true; //set flag to true
return false; //no need to continue loop if we found one
}
});
return inBounds;
}

Thank you for any insight.

geoco...@gmail.com

unread,
Nov 15, 2010, 9:44:28 PM11/15/10
to Google Maps JavaScript API v3
On Nov 15, 5:39 pm, CroNiX <cronix...@gmail.com> wrote:
> Is there a more efficient way to detect if any part of a polygon is in
> the current map bounds than to cycle through the polygons' points and
> manually test each point or is there a better way?  I did not see a
> native way to test a polygon like you can with a point using
> contains().  The docs say contains() requires a single latLng object
> as the parameter.  Its too bad it can't test whether the parameter is
> an array and do the same as below if it was, at least it doesn't seem
> so from the docs....
>
> I am currently doing this for my polygon detection, which works very
> well but looks like it can eat some horsepower if used on complex
> polygons, especially if tied to the bounds_changed event.
>
> function isPolyInBounds(map, polygon) {  //map obj, polygon obj
>     var currentBounds = map.getBounds();
>     var polyPoints = polygon.getPath();  //get path of current polygon
>     var inBounds = false;  //this will be set to true if at least 1
> point is in the current bounds
>

>
> Thank you for any insight.

I was intrigued by this:
http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLngBounds
intersects(other:LatLngBounds) boolean Returns true if this bounds
shares any points with this bounds.

I implemented it for the sidebar display here:
http://www.geocodezip.com/geoxml3_test/v3_geoxml3_us_counties01_highlight.html?lat=33.678198&lng=-114.15285124218751&zoom=8
[*** warning extremely complex KML, I don't recommend opening it in IE
***]

It seemed to do what I expected.

-- Larry
Reply all
Reply to author
Forward
0 new messages