使用地图绘制多边形区域范围,判断当前坐标是否在多边形区域范围内
//是否在多边形里
// $pt = [120.23,30.31]
// $polygon = [[120.23859800000002,30.304909],[120.23098800000002,30.295403],[120.22953100000001,30.291418],[120.22977400000002,30.287014],[120.24248399999999,30.287993],[120.24920400000002,30.290719],[120.25770399999999,30.2975],[120.26118600000001,30.303651],[120.26563799999997,30.303581],[120.26628599999998,30.307635],[120.26661000000001,30.313785],[120.25964699999997,30.312527],[120.24766599999998,30.306307]];
public function IsWithin($pt = [], $polygon = [])
{
$polygon = json_decode($polygon ,true);
$ptNum = count($polygon);
if ($ptNum < 3) {
return false;
}
$j = $ptNum - 1;
$zeroState = 0;
for ($k = 0; $k < $ptNum; $k++) {
$ptK = $polygon[$k];
$ptJ = $polygon[$j];
if ((($ptK[0] > $pt[0]) != ($ptJ[0] > $pt[0])) && ($pt[1] < ($ptJ[1] - $ptK[1]) * ($pt[0] - $ptK[0]) / ($ptJ[0] - $ptK[0]) + $ptK[1])) {
if ($ptK[0] > $ptJ[0]) {
$zeroState++;
} else {
$zeroState--;
}
}
$j = $k;
}
return $zeroState;
}
//返回$zeroState非零则代表在此区域范围内