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

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 method: https://developers.google.com/maps/documentation/javascript/reference. Is there a way to leverage this from PHP?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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";
?>

Solution 2

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;
  }

Solution 3

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.

Solution 4

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);

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply