google maps api : determine if point is inside shape

 ̄綄美尐妖づ 提交于 2020-01-06 13:52:16

问题


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);

}

回答1:


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);


来源:https://stackoverflow.com/questions/26413963/google-maps-api-determine-if-point-is-inside-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!