给定一对lat / lng值,如何确定该对是否在多边形内?我需要在
PHP中这样做.我看到Google Maps API有一个containsLocation方法:
https://developers.google.com/maps/documentation/javascript/reference.有没有办法利用PHP?
查找点是否在多边形中的一种方法是计算从点(在任何方向)绘制的线与多边形边界相交的次数.如果它们相交偶数次,则该点在外部.
原文链接:https://www.f2er.com/php/130827.html我已经在PHP中实现了这个Point in Polygon文章中的C代码,并使用下面的多边形来说明.
<?PHP //Point-In-Polygon Algorithm $polySides = 4; //how many corners the polygon has $polyX = array(4,9,11,2);//horizontal coordinates of corners $polyY = array(10,7,2,2);//vertical coordinates of corners $x = 3.5; $y = 13.5;//Outside //$y = 3.5;//Inside function pointInPolygon($polySides,$polyX,$polyY,$x,$y) { $j = $polySides-1 ; $oddNodes = 0; for ($i=0; $i<$polySides; $i++) { if ($polyY[$i]<$y && $polyY[$j]>=$y || $polyY[$j]<$y && $polyY[$i]>=$y) { if ($polyX[$i]+($y-$polyY[$i])/($polyY[$j]-$polyY[$i])*($polyX[$j]-$polyX[$i])<$x) { $oddNodes=!$oddNodes; }} $j=$i; } return $oddNodes; } if (pointInPolygon($polySides,$y)){ echo "Is in polygon!"; } else echo "Is not in polygon"; ?>