I use this in my app to determine if a vehicle is within a defined zone made of 5 or more lat/lon coordinates. Point 1 is also Point 5 (first point and last point are same value).
A simple box shows how to create this:
1----------2
| ........... |
| ........... | clockwise (or counter clockwise)
| ........... |
4----------3
In my case, I determine either a) the speed of the vehicle or b) how long stopped in zone.
Note: Larger zones work best due to the inaccuracy of GPS. My accuracy reading jumps
from 6 meters to 24 or more meters...
Source: Klaus from post #6 below
This function is tested and works fine.
A simple box shows how to create this:
1----------2
| ........... |
| ........... | clockwise (or counter clockwise)
| ........... |
4----------3
In my case, I determine either a) the speed of the vehicle or b) how long stopped in zone.
Note: Larger zones work best due to the inaccuracy of GPS. My accuracy reading jumps
from 6 meters to 24 or more meters...
Source: Klaus from post #6 below
This function is tested and works fine.
B4X:
Sub FindInZone( polx As List, poly As List, x As Double, y As Double) As Boolean
' polx = list of lats for polygon
' poly = list of lons for polygon
' y = lon to test
' x = lat to test
Dim x1, y1, x2, y2, D As Double
Dim i, ni As Int
ni = 0
x1 = polx.Get(0)
y1 = poly.Get(0)
For i = 0 To polx.Size -1
If i < polx.Size Then
x2 = polx.Get(i)
y2 = poly.Get(i)
Else
x2 = polx.Get(0) ' checks the last line
y2 = poly.Get(0)
End If
If y >= Min(y1, y2) Then
If y <= Max(y1, y2) Then
If x <= Max(x1, x2) Then
If (x = x1 AND y = y1) OR (x = x1 AND x = x2) Then ' checks vertices and vertical lines
Return True
End If
If y1 <> y2 Then
D = (y - y1) * (x2 - x1) / (y2 - y1) + x1
If x1 = x2 OR x <= D Then
ni = ni + 1
End If
End If
End If
End If
End If
x1 = x2
y1 = y2
Next
If ni Mod 2 = 0 Then
Return False
Else
Return True
End If
End Sub
Last edited: