如何在Google地图几何库中使用containsLocation.

Neo*_*734 4 google-maps google-maps-api-3

我正在尝试使用containsLocationGoogle几何图书库,但无法让它工作......

var point = new google.maps.LatLng(51.331, 3.538);
var poly = [
 new google.maps.LatLng(51.401818509550615, 3.547626782103622),
 new google.maps.LatLng(51.397574277049365, 3.563607598960424),
 new google.maps.LatLng(51.398540111384975, 3.567880788749134),
 ... // it is a lot bigger
];

if(google.maps.geometry.poly.containsLocation(point, poly) == true) {
 alert("yes");
}
Run Code Online (Sandbox Code Playgroud)

Javascript控制台出错,但这指向Google的lib中的一个函数.所以我认为问题应该在这个函数的某个地方.

Neo*_*734 5

哦,傻我

我使用所有的coords作为数组是错误的,我不得不使用创建的多边形对象.

var polyOptions = {
 ...
}

draw = new google.maps.Polygon(polyOptions);
draw.setMap(map);

if(google.maps.geometry.poly.containsLocation(point, draw) == true) {
 alert("yes");
}
Run Code Online (Sandbox Code Playgroud)


小智 5

我有同样的问题([object Object]),我可以解决创建多边形变量的问题: draw = new google.maps.Polygon({paths:polyOptions});

然后问题就消失了。