turbo2oh
turbo2oh

Reputation: 2879

google maps api : determine if point is inside shape

I have created a rectangle on the map along with a listener for bounds_changed. It calls a method to determine if a point is within the adjusted rectangle. However I'm getting the error Cannot read property 'getLength' of undefined when I change the size of the rectangle. This error is coming from the call to containsLocation.

In my init:

rectangle = new google.maps.Rectangle({
                bounds: bounds,
                editable: true,
                draggable: true,
                geodesic: true,
                map: map
            });


google.maps.event.addListener(rectangle , 'bounds_changed', isWithinPoly);

function to determine if point falls in adjusted rectangle:

function isWithinPoly(){
    console.log(rectangle);
    var point = new google.maps.LatLng(51.331, 3.538);
    var isWithinPolygon = google.maps.geometry.poly.containsLocation(point, rectangle);
    console.log(isWithinPolygon);

}

Upvotes: 0

Views: 2697

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117314

containsLocation expects a polygon as argument, not a rectangle.

For a rectangle use the method contains of the bounds:

var isWithinRectangle = rectangle.getBounds().contains(point);

Upvotes: 5

Related Questions