3
votes

With the help of @Dr.Molle answer I learnt to do free hand drawing in Google maps. Now I'm trying to get the polygon drawn within a polygon something like in the below SS

enter image description here

I want to get the polygons marked in yellow and green within the black.

I'm not sure whether this is possible or not. Please shed some light on this issue.

Updates: on further research I learnt about a method called containsLocation(point, polygons) which is used to find whether the given lat/lng point is within the polygon or not.

But sadly there is no default method to check polygons within polygon provided by Google maps :(

3

3 Answers

6
votes

You can check if a polygon is within another polygon by looping through each point of the inner polygon and testing if it is contained within the outer polygon using containsLocation().

var isPolygonInsidePolygon = function (innerPolygon, outerPolygon) {
    var pointsInside = 0;
    var pointsOutside = 0;
    innerPolygon.getPath().getArray().map(function (x) { 
        (google.maps.geometry.poly.containsLocation(x, outerPolygon)) ? pointsInside++ : pointsOutside++;
    });
    return (pointsOutside > 0) ? false : true;
};

The JavaScript map() function may not work in older browsers, IE8 or lower.

0
votes

This is a GIS question. Google Maps API isn't really a full-blown GIS. If you want an open-source solution, I suggest loading your yellow and green polygons into a PostGIS database. Then you can query the database.

As an example, you can encode the drawn polygon as a POLYGON object which has the format:

POLYGON((lon lat, lon lat, lon lat, lon lat, ... lon lat))

And then send that to a PHP file from javascript like (you'll wrap this in a $.get() command or similar and return json results:

getParcels.php?bounds=POLYGON((lon lat, lon lat, lon lat, lon lat, ... lon lat))

In the PHP file, query the PostGIS database and return the ids of the yellow and green polygons:

<?php
$pgcon = pg_connect ("dbname=gis user=gisuser connect_timeout=5") or die ( 'Can not connect to PG server' );
if (!$pgcon) {
  echo "No connection to GIS database.\n";
}
$bounds = urldecode($_GET["bounds"];
$ewkt = 'SRID=4326;' . $bounds);
$json = ''; // this will contain your output
// Here I am returning the polygon geometry and the parcelID...
$query .= <<<EOD
SELECT 
  ST_AsGeoJSON(the_geom) as geom, 
  parid 
FROM
  parcels
WHERE 
  ST_Intersects(the_geom, ST_GeomFromEWKT( $1 ));
EOD;

$result = pg_query_params($pgcon, $query, array($ewkt));

if($result) {
$json = '{"type":"FeatureCollection", "features":[';
    while($row = pg_fetch_assoc($result)) {
        $json .= '{"geometry":' . $row['geom'] . ',';
        $json .= '"type":"Feature","properties":{"parid":"' . $row['parid'] . '"}},';
    }
    $json = substr($json, 0,-1).']}';
}

echo $json;
?>

This will return the parcels that intersect your polygon using the ST_Intersects command in PostGIS.

0
votes

An alternative implementation working from @chris-smith solution that might be faster, since it doesn't keep looping if it finds an outside point:

function isPolygonInsidePolygon( innerPolygon, outerPolygon ) {

  var points = innerPolygon.getPath().getArray();

  for( var i = 0; i < points.length; i++ ){
    if( ! google.maps.geometry.poly.containsLocation( points[i], outerPolygon) ){ 
      return false; 
    }
  }

  return true; 

}