I've searched for a long time and haven't quite found the answer. There are several that come close, but nothing that seems to exactly answer my question! I would like to take a set of coordinates, create a polygon out of them, and determine if a given location is inside that polygon. Any help would be very appreciated!
-
This is not a simple problem. Lat/long points aren't in euclidean space. You'll something like a convex hull formula in spherical space but not quite – Falmarri Jan 27 '11 at 03:08
-
1This is true near the poles and over large areas. If the problem only concerns relatively small areas away from the poles, then a euclidean approximation may be fine. – Ted Hopp Jan 27 '11 at 03:23
2 Answers
Here http://alienryderflex.com/polygon you find a very clever algorithm and some C code that's very close to Java which does the job (euclidean approximation). So I'd just write my own class since:
The
contains()
of Android Region doesn't give you an accurate value. If it returns true, the point to check is in the polygon. So far, so good. If it returns false, however, it could still be inside. I somehow wonder how that method deserves to be calledcontains()
because it's basically useless for most purposes.Apparently you can't use the Java AWT Polygon class in Android.
Okay -- a complete rewrite of the answer I just posted (which I hope nobody read).
Construct a Path for the polygon, set it as the Path for a Region, and call the region's contains() method to test the point.

- 232,168
- 48
- 399
- 521