# Check if lat/long point is within a set of polygons using google maps

I know how to do the above using Esri (Query task on shapefile), but can this also be done using Google Maps? Do I have to query each polygon or is there one method to query a set of polygons?

-

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:

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);
if (polygon.Contains(point)) {
// point is inside polygon
}

-
Yes, ray casting is the way to go I think. I actually utilized that algorithm myself via a python script. My implementation can be found here: gramsky.blogspot.com/2012/12/… – ngramsky Jan 20 '13 at 23:36
I had to remove the bits that added 360 to negative coordinates for this to work properly around longitude 0. – user1431317 Apr 15 at 11:02

I'd go the Open Layers plugin; fetch it, and you can then even add whatever dynamic layer to your map and export.

*Before doing so, ensure you have your project CRS(EPSG) set to WGS84, and that 'on the fly' CRS transformation is enabled under your Project Properties settings.

Hope this helps.

-

if you use third party program as geodjango, you can check out your point whether within a set of polygons or not. more info is here.

contains

Availability: PostGIS, Oracle, MySQL, SpatiaLite

Tests if the geometry field spatially contains the lookup geometry.

Example:

Zipcode.objects.filter(poly__contains=geom)

Backend     SQL Equivalent
PostGIS     ST_Contains(poly, geom)
Oracle  SDO_CONTAINS(poly, geom)
MySQL   MBRContains(poly, geom)
SpatiaLite  Contains(poly, geom)

i hope it helps you...

-

I know this is a bit late but It might helpful in case SomeOne didn't try Android-Map-Utils library. I have posted this Question's specific methods only for Android. Here you go:

http://stackoverflow.com/a/31988461/2054348

-