<?php
//计算坐标是否在某区域内
function isPointInPolygon($point, $polygon) {
list($px, $py) = $point;
$inside = false;
$n = count($polygon);
$j = $n - 1;
for ($i = 0; $i < $n; $i++) {
list($ix, $iy) = $polygon[$i];
list($jx, $jy) = $polygon[$j];
$intersect = (($iy > $py) != ($jy > $py)) &&
($px < ($jx - $ix) * ($py - $iy) / ($jy - $iy) + $ix);
$j = $i;
if ($intersect) {
$inside = !$inside;
}
}
return $inside;
}
// 使用示例
$point = [116.391, 39.906]; // 经度,纬度
$polygon = [
[116.385, 39.902], // 多边形顶点列表,经度,纬度
[116.395, 39.908],
[116.405, 39.902],
[116.395, 39.895]
];
$polygon = [
[119.925687,31.764112],
[120.274503,31.7501],
[120.209958,31.586466],
[119.836423,31.530297],
[119.624936,31.734917],
[119.700467,31.923935],
[119.911954,32.001996]
];
$point = [120.010831,31.871469]; //不在
$point = [119.627683,31.557216]; //不在
$point = [119.777372,31.77462]; //在
if (isPointInPolygon($point, $polygon)) {
echo "点在多边形内";
} else {
echo "点不在多边形内";
}