Point in Polygon Function???

3,457 views
Skip to first unread message

taylor

unread,
Jun 13, 2010, 2:58:23 AM6/13/10
to Google Maps JavaScript API v3

I am trying to determine if a latitude longitude point is in a
polygon. Classic point in polygon issue. I see google has build a
solutions because they can detect if a point click is in polygon. I
would assume i can step backwards and little and detect if a lat,long
point is in the polygon(i just dont know how). basically i have array
of points aka "bermudaTriangl" and a latlong point- but i want to get
true or false with no click.

Any suggestions???


// Add a listener for the click event
google.maps.event.addListener(bermudaTriangle, 'click', showArrays);

Rossko

unread,
Jun 13, 2010, 5:30:47 AM6/13/10
to Google Maps JavaScript API v3
> Any suggestions???

You could search this group for "point in polygon". Example -
http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/734bbdd35614fefa/b6f78c71d3dc3846

ClubPit

unread,
Jun 13, 2010, 6:44:31 AM6/13/10
to Google Maps JavaScript API v3
> Any suggestions???

There is a v2 solution you can use here :

http://econym.org.uk/gmap/inside.htm

Despite it being for a now redundant click method you can tweak it to
work with v2 to decide if any LatLng is within a polygon.

If your on v3 then I've tweaked it further to still work :

google.maps.Polygon.prototype.Contains = function(latLng)
{
var j = 0;
var oddNodes = false;
var x = latLng.lng();
var y = latLng.lat();
for (var i = 0; i < this.getPath().getLength(); i++) {
j++;
if (j == this.getPath().getLength()) { j = 0; }
if (((this.getPath().getAt(i).lat() < y) &&
(this.getPath().getAt(j).lat() >= y))
|| ((this.getPath().getAt(j).lat() < y) &&
(this.getPath().getAt(i).lat() >= y))) {
if (this.getPath().getAt(i).lng() + (y -
this.getPath().getAt(i).lat())
/ (this.getPath().getAt(j).lat() -
this.getPath().getAt(i).lat())
* (this.getPath().getAt(j).lng() -
this.getPath().getAt(i).lng()) < x) {
oddNodes = !oddNodes
}
}
}
return oddNodes;
}


var myPolgyon = new google.maps.Polygon({...});
var myLatLng = new google.maps.LatLng(52.222594,-0.966969);

if (myPolygon.Contains(myLatLngt))
{
alert("It's in the polygon");
}
else
{
alert("It's outside the polygon");
}

A working example of this can be found here :

http://selfserve.northampton.gov.uk/nbc/MyCouncil.html

Hopefully there's a cleaner method out there, but this was the best I
could come up with.

Cheers,

KW

Enoch Lau (Google Employee)

unread,
Jan 13, 2013, 6:09:20 PM1/13/13
to google-map...@googlegroups.com, th7...@gmail.com
There is google.maps.geometry.poly.containsLocation:

https://developers.google.com/maps/documentation/javascript/reference#poly

On Thursday, January 10, 2013 11:59:54 PM UTC+11, Abhishek Gahlout wrote:

The Google maps API does not already provide a method for checking points in polygons. After researching a bit I stumbled across the Ray-casting algorithm which will determine if an X-Y coordinate is inside a plotted shape. This will translate to latitude and longitude. The following extends the google.maps.polygon.prototype to use this algorithm. Simply include this code at a point in the code after google.maps has loaded:

google.maps.Polygon.prototype.Contains = function(point) { var crossings = 0, path = this.getPath();

    // for each edge
    for (var i=0; i < path.getLength(); i++) {
        var a = path.getAt(i),
            j = i + 1;
        if (j >= path.getLength()) {
            j = 0;
        }
        var b = path.getAt(j);
        if (rayCrossesSegment(point, a, b)) {
            crossings++;
        }
    }

    // odd number of crossings?
    return (crossings % 2 == 1);

    function rayCrossesSegment(point, a, b) {
        var px = point.lng(),
            py = point.lat(),
            ax = a.lng(),
            ay = a.lat(),
            bx = b.lng(),
            by = b.lat();
        if (ay > by) {
            ax = b.lng();
            ay = b.lat();
            bx = a.lng();
            by = a.lat();
        }
        // alter longitude to cater for 180 degree crossings
        if (px < 0) { px += 360 };
        if (ax < 0) { ax += 360 };
        if (bx < 0) { bx += 360 };

        if (py == ay || py == by) py += 0.00000001;
        if ((py > by || py < ay) || (px > Math.max(ax, bx))) return false;
        if (px < Math.min(ax, bx)) return true;

        var red = (ax != bx) ? ((by - ay) / (bx - ax)) : Infinity;
        var blue = (ax != px) ? ((py - ay) / (px - ax)) : Infinity;
        return (blue >= red);

    }

 };

Here we have extended the functionality of google.maps.Polygon by defining a function with name ‘Contains’ which can be used to determine whether the latitude longitude provided in function parameter are within the polygon or not. Here we make use of Ray-casting algorithm and developed a function using the same. After doing this much of exercise now, we can check a point as follows:

var point = new google.maps.LatLng(52.05249047600099, -0.6097412109375); var polygon = new google.maps.Polygon({path:[INSERT_PATH_ARRAY_HERE]}); if (polygon.Contains(point)) { // point is inside polygon }

For complete code and demo please go to: http://counsellingbyabhi.blogspot.in/2013/01/google-map-check-whether-point-latlong.html


Reply all
Reply to author
Forward
0 new messages