My favorites | Sign in
Project Home Issues
New issue   Search
for
  Advanced search   Search tips   Subscriptions
Issue 1978: Add getBounds() to Polyline/Polygon objects
34 people starred this issue and may be notified of changes. Back to list
Status:  Fixed
Owner:  [email protected]
Closed:  May 2012


Sign in to add a comment
 
Reported by [email protected], Nov 30, 2009
Many people use the polygon for outlining important geographic regions as
county's etc, voting districts etc. 

Therefore it would be nice of determining whether a point is within the
bounds of a certain polygon or even better and faster would be determining
whether a point in fact is in the bounds of any polygon on the map and
which polygon.

An example of usage would be determining where my voting ballot is. Just
search my home adress and find the voting ballot that i should go to.

With the current V3 you have to first use a array of LatLng to construct a
polygon, then create a LatLngBounds, iterate all the points in the Polygon
with LatLngBounds.extend() then test if the point (home adress) is
contained in the LatLngBounds. And you have to do this procedure for every
ballot district you might have in your database.

Kind of make my life a bitch when i have over 5000 ballots in my city, and
my job is to make a search-able map of them.
Jan 6, 2010
#1 [email protected]
Thanks for reporting.  It's a valid suggestion and use case.

Would adding a "getBounds" method to the Polyline/Polygon object suffice?

Status: Started
Owner: [email protected]
Jan 18, 2010
#2 [email protected]
getBounds would suffice in most cases, but the problem is that Bounds are
rectangular, and geografic regions rearly are rectangular and therefore would the
getBounds method crudly tell whether a point is in the polygon or not. 

Is there a more precise method to determine this?
Jan 21, 2010
#3 [email protected]
getBounds would actually suffice.

V2 equivalent methon of Point in Polygon: 
// Create polygon method for collision detection
GPolygon.prototype.containsLatLng = function(latLng) {
    // Do simple calculation so we don't do more CPU-intensive calcs for obvious misses
    var bounds = this.getBounds();
   
    if(!bounds.containsLatLng(latLng)) {
        return false;
    }
   
    var numPoints = this.getVertexCount();
    var inPoly = false;
    var i;
    var j = numPoints-1;
   
    for(var i=0; i < numPoints; i++) {
        var vertex1 = this.getVertex(i);
        var vertex2 = this.getVertex(j);
       
        if (vertex1.lng() < latLng.lng() && vertex2.lng() >= latLng.lng() ||
vertex2.lng() < latLng.lng() && vertex1.lng() >= latLng.lng())  {
            if (vertex1.lat() + (latLng.lng() - vertex1.lng()) / (vertex2.lng() -
vertex1.lng()) * (vertex2.lat() - vertex1.lat()) < latLng.lat()) {
                inPoly = !inPoly;
            }
        }
       
        j = i;
    }
   
    return inPoly;
};
Feb 2, 2010
#4 [email protected]
Okay, thanks for confirming.

I've shared your request to add a getBounds method to both Polyline and Polygon objects.
Summary: Add getBounds() to Polyline/Polygon objects
Status: Acknowledged
Labels: Internal-2414184
Feb 17, 2010
#5 [email protected]
Hanif, instead of adding a getBounds method, you can construct your own LatLngBounds
by using the extend() method against your Polyline/Polygon paths.  Then you can call
LatLngBounds.contains() to determine whether a point exists within.
Status: WontFix
Feb 17, 2010
#6 [email protected]
Would that affect the performance? It would have to iterate throu all of the points
in the polygon, or would the getBounds() method have the same logic and performance?
Feb 17, 2010
#7 [email protected]
A getBounds() method would essentially be doing the same thing.

Are you trying to determine if a point exists within an enclosed polyline or polygon?
 Reason I ask is because LatLngBounds are always in the shape of a rectangle.  If
you're trying to determine whether a point exists within a triangular polygon, using
LatLngBounds would not work correctly since the rectangular shape would include areas
outside the shape.
Feb 17, 2010
#8 [email protected]
(No comment was entered for this change.)
Status: NeedsMoreInfo
Feb 17, 2010
#9 [email protected]
The answer to the last question would slightly modify this feature request to be more
specific.  For example, a more descriptive function name for the proposed
functionality would be Polygon.isLatLngInside or Polyline.isLatLngNear or something
of the sort.
Feb 17, 2010
#10 [email protected]
(No comment was entered for this change.)
Labels: -Internal-2414184 Internal-2451527
Jun 29, 2010
#11 [email protected]
(No comment was entered for this change.)
Owner: thor.mitchell
Jul 1, 2010
#12 [email protected]
(No comment was entered for this change.)
Jul 15, 2010
#13 eros%[email protected]
If you can't wait for Google solution, here is mine:

if (!google.maps.Polyline.prototype.getBounds)
google.maps.Polyline.prototype.getBounds = function() {

  var path = this.getPath();
  
  var slat, blat = path.getAt(0).lat();
  var slng, blng = path.getAt(0).lng();

  for (var i = 1; i < path.getLength(); i++) {
    var e = path.getAt(i);
    slat = ((slat < e.lat())?slat:e.lat());
    blat = ((blat > e.lat())?blat:e.lat());
    slng = ((slng < e.lng())?slng:e.lng());
    blng = ((blng > e.lng())?blng:e.lng());
  }      

  return new google.maps.LatLngBounds(new google.maps.LatLng(slat, slng), new google.maps.LatLng(blat, blng));
  
}
Jul 19, 2010
#14 eros%[email protected]
Simplier version:

if (!google.maps.Polyline.prototype.getBounds)
google.maps.Polyline.prototype.getBounds = function() {
  
  var bounds = new google.maps.LatLngBounds();
   
  this.getPath().forEach( function(latlng) { bounds.extend(latlng); } ); 
  
  return bounds; 
  
}
Aug 30, 2010
Project Member #15 [email protected]
(No comment was entered for this change.)
Status: Acknowledged
Labels: -Internal-2451527 Internal-2962153
Aug 30, 2010
Project Member #16 [email protected]
(No comment was entered for this change.)
Labels: -Internal-2962153 Internal-1814010
Oct 30, 2011
#18 [email protected]
(No comment was entered for this change.)
Labels: -Internal-1814010 Internal-2451527
Oct 30, 2011
#19 [email protected]
 Issue 2451  has been merged into this issue.
May 21, 2012
#20 [email protected]
(No comment was entered for this change.)
Status: Fixed
Sign in to add a comment

Powered by Google Project Hosting