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.

karthik_ppts 81 Posting Pro

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.

madCoder 17 Junior Poster

karthik_ppts 81 Posting Pro

Ok.. Thanks for your reply..

pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

karthik_ppts 81 Posting Pro

karthik_ppts 81 Posting Pro

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

Edited by karthik_ppts because: * n/a *

pritaeas commented: Thanks for sharing +13

madCoder 17 Junior Poster

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

gaalvarez 0 Newbie Poster

msgrasser 0 Newbie Poster

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

Edited by msgrasser because: * formatting & clarity *

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.