I. Background:
How to determine whether a specified latitude and longitude point falls in a polygon region.
Second, the Implementation Code (Delphi)
Code
Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/
-->type
Tmypoint = Packed record
x:double;
y:double;
End
{*------------------------------------------------------------------------------
Determines whether the specified latitude and longitude coordinate point falls within the specified polygon region
@param the longitude of the ALon point
@param the latitude of the Alat point
@param apoints Specifies the coordinates of each node of the polygon region
@return True falls within range False is not in range
------------------------------------------------------------------------------*}
function Isptinpoly (ALon, alat:double; Apoints:array of Tmypoint): Boolean;
Var
Isum, icount, Iindex:integer;
DLon1, DLon2, DLAT1, DLAT2, dlon:double;
Begin
Result: = False;
if (Length (apoints) < 3) Then
Begin
Result: = False;
Exit;
End
Isum: = 0;
Icount: = Length (apoints);
For Iindex: =0 to ICount-1 do
Begin
if (Iindex = iCount-1) Then
Begin
DLon1: = Apoints[iindex]. X
DLAT1: = Apoints[iindex]. Y
DLon2: = Apoints[0]. X
DLAT2: = Apoints[0]. Y
End
Else
Begin
DLon1: = Apoints[iindex]. X
DLAT1: = Apoints[iindex]. Y
DLon2: = Apoints[iindex + 1]. X
DLAT2: = Apoints[iindex + 1]. Y
End
if ((Alat >= dLat1) and (Alat < DLAT2)) or ((ALAT>=DLAT2) and (Alat < DLAT1)) then
Begin
if (ABS (DLAT1-DLAT2) > 0) Then
Begin
Dlon: = DLon1-((dlon1-dlon2) * (Dlat1-alat))/(DLAT1-DLAT2);
if (Dlon < ALon) Then
INC (isum);
End
End
End
if (isum mod 2 <> 0) Then
Result: = True;
End