谷歌地图api:确定点是否在形状内部

google maps api : determine if point is inside shape

本文关键字:内部 是否 api 谷歌地图      更新时间:2023-09-26

我已经在映射上创建了一个矩形以及bounds_changed的侦听器。它调用一个方法来确定一个点是否在调整后的矩形内。然而,当我更改矩形的大小时,我得到了错误Cannot read property 'getLength' of undefined。此错误来自对containsLocation的调用。

在我的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 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);
}

containsLocation需要一个多边形作为参数,而不是矩形。

对于矩形,使用边界的方法contains

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