Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending Bounds For Polygons -- Google Maps V3 Javascript API

My goal is to detect if a marker is within an oddly shaped polygon object (think 25 different points). I have noticed that the LatLngBounds property, only contains two sets of LatLngs, even after using the .extend function, for example:

((34.347971491244955, -122.40966796875), (34.829586636768205, -120.9649658203125))

Is this normal behavior?

The code below is a tiny snippet of the program I am building, but contains the relevant information for how I am using the extend function. Does anyone have any idea how to detect if a marker is within an oddly shaped polygon, if I can't use a combination of .extend() and .contains()?

var coordinates = (polygon.getPath().getArray()); //get            
var bounds = new google.maps.LatLngBounds();


function extendBounds(bounds, coordinates) {
    for (b in coordinates) {
        bounds.extend(coordinates[b]);            
    };
    console.log(bounds.toString());
};
like image 222
Ray Suelzer Avatar asked Aug 10 '26 02:08

Ray Suelzer


1 Answers

The bounds of a polygon is a rectangle. If you want to detect if a marker is in the polygon use the containsLocation function in google.maps.geometry.poly namespace, passing it the polygon and the position of the marker

containsLocation(point:LatLng, polygon:Polygon) | boolean | Computes whether the given point lies inside the specified polygon.

like image 104
geocodezip Avatar answered Aug 11 '26 21:08

geocodezip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!