Hi Frendz,
I have a set of polygon points on google map. I need to find whether the given input point is lying inside of the polygon or not? If anybody found articles related this please guide me.

And also i need the same with poly line.

I haven't tried this in PHP so I don't have any specifics. I usually work with more simple bounding boxes (in Actionscript 3.) In math, what you seek is called a "Point in Polygon Test." Something you can search on at least.

I haven't tried this in PHP so I don't have any specifics. I usually work with more simple bounding boxes (in Actionscript 3.) In math, what you seek is called a "Point in Polygon Test." Something you can search on at least.

Ok.. Thanks for your reply..

Thanks for all replies. I got this this code from google search and this is working fine.

$vertices_x = array(37.628134, 37.629867, 37.62324, 37.622424);    // x-coordinates of the vertices of the polygon
$vertices_y = array(-77.458334,-77.449021,-77.445416],-77.457819]; // y-coordinates of the vertices of the polygon
$points_polygon = count($vertices_x);  // number vertices
$longitude_x = $_GET["longitude"];  // x-coordinate of the point to test
$latitude_y = $_GET["latitude"];    // y-coordinate of the point to test

if (is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude, $latitude)){
  echo "Is in polygon!";
}
else echo "Is not in polygon";


function is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y)
{
  $i = $j = $c = 0;
  for ($i = 0, $j = $points_polygon ; $i < $points_polygon; $j = $i++) {
    if ( (($vertices_y[$i]  >  $latitude_y != ($vertices_y[$j] > $latitude_y)) &&
     ($longitude_x < ($vertices_x[$j] - $vertices_x[$i]) * ($latitude_y - $vertices_y[$i]) / ($vertices_y[j] - $vertices_y[$i]) + $vertices_x[$i]) )
       $c = !$c;
  }
  return $c;
}
commented: Thanks for sharing +13

Thanks, Karthik, for posting that code you found!

It had some syntax errors and generated notices/warnings so for future reference here is a cleaned up version:

<?php
/**
  From: http://www.daniweb.com/web-development/php/threads/366489
  Also see http://en.wikipedia.org/wiki/Point_in_polygon
*/
$vertices_x = array(37.628134, 37.629867, 37.62324, 37.622424); // x-coordinates of the vertices of the polygon
$vertices_y = array(-77.458334,-77.449021,-77.445416,-77.457819); // y-coordinates of the vertices of the polygon
$points_polygon = count($vertices_x); // number vertices
$longitude_x = $_GET["longitude"]; // x-coordinate of the point to test
$latitude_y = $_GET["latitude"]; // y-coordinate of the point to test

//// For testing.  This point lies inside the test polygon.
// $longitude_x = 37.62850;
// $latitude_y = -77.4499;
 
if (is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y)){
  echo "Is in polygon!";
}
else echo "Is not in polygon";
 
 
function is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y)
{
  $i = $j = $c = 0;
  for ($i = 0, $j = $points_polygon-1 ; $i < $points_polygon; $j = $i++) {
    if ( (($vertices_y[$i] > $latitude_y != ($vertices_y[$j] > $latitude_y)) &&
    ($longitude_x < ($vertices_x[$j] - $vertices_x[$i]) * ($latitude_y - $vertices_y[$i]) / ($vertices_y[$j] - $vertices_y[$i]) + $vertices_x[$i]) ) ) 
        $c = !$c;
  }
  return $c;
}
?>

So I know this question has already been solved, but I wanted to also leave a Java sample for reference. For my application, I had implemented a custom Object called GeogatePoint that contains some information. For the purpose of this example though, it can be considered as an object simply containing the lat and lon for a given boundary point. As such, geogate_points is a list of all these points making up the geogate boundary. Hope this helps someone!

public class Geogate 
{

    List<GeogatePoint> geogate_points;

    public Geogate(List<GeogatePoint> geogate_points) 
    {
        this.geogate_points = geogate_points;
    }

    public boolean contains(double lat, double lon) 
    {
        boolean c = false;

        int i = 0;
        int j = geogate_points.size()-1;

        for (GeogatePoint point : geogate_points) {

            double lat_i = point.lat;
            double lon_i = point.lon;
            double lat_j = geogate_points.get(j).lat;
            double lon_j = geogate_points.get(j).lon;

            if ( ((lat_i  >  lat != (lat_j > lat)) &&
                    (lon < (lon_j - lon_i) * (lat - lat_i) / (lat_j - lat_i) + lon_i) ) )
                    c = !c;
            j=i++;
        }
        return c;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.