Google Map: is a lat/lng within a polygon?

后端 未结 4 1671
-上瘾入骨i
-上瘾入骨i 2020-12-06 06:44

Given a pair of lat/lng values, how do I determine if the pair is within a polygon? I need to do this in PHP. I see that Google Maps API has a containsLocation

4条回答
  •  鱼传尺愫
    2020-12-06 07:16

    One way to find if a point is in a polygon is to count how many times a line drawn from the point (in any direction) intersects with the polygon boundary. If they intersect an even number of times, then the point is outside.

    I have implemented the C code from this Point in Polygon article in php and used the polygon below to illustrate.

    polygon

    =$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,$polyX,$polyY,$x,$y)){
      echo "Is in polygon!";
    }
    else echo "Is not in polygon";
    ?>
    

提交回复
热议问题