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

后端 未结 4 1666
-上瘾入骨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 06:57

    As you said in your question, this method is related to the geometric library of the Google Maps API V3, so the objects cannot be handle from PHP, the solution that could works it's to make an ajax called to the server, asking for your lat and long, returns the data (go to the data base do your stuff) and in the event success (with the library of google maps already loaded) check if the points contains that point. And maybe if you need to save this in some point make another ajax sending the result for the current position asked.

    0 讨论(0)
  • 2020-12-06 07:02

    There are a copuple of ways to do this I think. The first would be to use this extension or something like this to determine if the point is inside the polygon:

    Google-Maps-Point-in-Polygon Extension

    Here is an explanation on the Ray casting algorithm that should help you out a little too:

    Point in polygon

    The simple example from the extension shows it is pretty straight forward:

            var coordinate = new google.maps.LatLng(40, -90);                                                                                                                                                                                                       
            var polygon = new google.maps.Polygon([], "#000000", 1, 1, "#336699", 0.3);
            var isWithinPolygon = polygon.containsLatLng(coordinate);
    
    0 讨论(0)
  • 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

    <?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,$polyX,$polyY,$x,$y)){
      echo "Is in polygon!";
    }
    else echo "Is not in polygon";
    ?>
    
    0 讨论(0)
  • 2020-12-06 07:19

    very big thanks to David Strachan and Darel Rex Finley

    i want to share my php version, is slightly different beacause it takes the point as an array ([lat, lng]) and the polygon as an array of point ([[lat, lng],[lat, lng],...])

      function pointInPolygon($point, $polygon){//http://alienryderflex.com/polygon/
         $return = false;
         foreach($polygon as $k=>$p){
            if(!$k) $k_prev = count($polygon)-1;
            else $k_prev = $k-1;
    
            if(($p[1]< $point[1] && $polygon[$k_prev][1]>=$point[1] || $polygon[$k_prev][1]< $point[1] && $p[1]>=$point[1]) && ($p[0]<=$point[0] || $polygon[$k_prev][0]<=$point[0])){
               if($p[0]+($point[1]-$p[1])/($polygon[$k_prev][1]-$p[1])*($polygon[$k_prev][0]-$p[0])<$point[0]){
                  $return = !$return;
               }
            }
         }
         return $return;
      }
    
    0 讨论(0)
提交回复
热议问题