Possible to Determine if Point is within Shape?

60 views
Skip to first unread message

Zachary DeBruine

unread,
Nov 18, 2011, 7:43:17 PM11/18/11
to google-map...@googlegroups.com
Given a latitude-longitude point, is there an easy code that I can use to determine whether or not that point is within the bounds of the user-defined geometrical shape?

Thanks!
-Zach

Rossko

unread,
Nov 19, 2011, 6:51:39 AM11/19/11
to Google Maps JavaScript API v3
> Given a latitude-longitude point, is there an easy code that I can use to
> determine whether or not that point is within the bounds of the
> user-defined geometrical shape?

Search for 'point in polygon'
useful examples
http://alienryderflex.com/polygon/
http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/b2aeb4f444dbe410/e159b0f5c9248c09

robert

unread,
Nov 19, 2011, 12:01:32 AM11/19/11
to Google Maps JavaScript API v3
Yes it is possible, I was as stuck as you probably were back in the v2
days and tried a million and a have different ways of figuring this
out when I come across a solution, I wish I remembered where from so I
could attribute it to them. However I did port it to v3, it involves
extending the Polygon class to include a function called Contains,
which takes in the latlng object and returns back true or false.

The function is as follows:

google.maps.Polygon.prototype.Contains = function(point) {

var j = 0;
var func_oddNodes = false;
var x = point.lng();
var y = point.lat();
var poly = this.getPath();

for (var i=0; i < poly.getLength(); i++) {

j++;

if( j == poly.getLength() ) {
j = 0;
} // end if


if ( ((poly.getAt(i).lat() < y) && (poly.getAt(j).lat() >= y))
|| ((poly.getAt(j).lat() < y) && (poly.getAt(i).lat() >= y)) ) {


if ( poly.getAt(i).lng() + (y - poly.getAt(i).lat())
/ (poly.getAt(j).lat()-poly.getAt(i).lat())
* (poly.getAt(j).lng() - poly.getAt(i).lng())<x ) {

func_oddNodes = !func_oddNodes

} // end if


} // end if

} // end for

return func_oddNodes;

} // end function


And to make the call to it:

mypolygon.Contains(mypoint);


I have it on a site that is up for now, (I'm working to replace it
soon) its @ http://www.cadistrict38.org/new/2011/m.php, just click
submit on that first pane and then enter in any name and try location
"Lakewood, CA" and it should spit back "within Lakewood"

Hope this helps!


On Nov 18, 4:43 pm, Zachary DeBruine <zacharydebru...@gmail.com>
wrote:

geoco...@gmail.com

unread,
Nov 19, 2011, 8:33:31 PM11/19/11
to Google Maps JavaScript API v3
On Nov 19, 12:01 am, robert <rsteb...@gmail.com> wrote:
> Yes it is possible, I was as stuck as you probably were back in the v2
> days and tried a million and a have different ways of figuring this
> out when I come across a solution, I wish I remembered where from so I
> could attribute it to them.

That is from Mike Williams' v2 tutorial:
http://econym.org.uk/gmap/

Third Party Extensions
Part 12 Using EPoly - extra methods for polygons and polylines.
http://econym.org.uk/gmap/epoly.htm

There are several ports of it to v3.

-- Larry

> soon) its @http://www.cadistrict38.org/new/2011/m.php, just click

Reply all
Reply to author
Forward
0 new messages