Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use containsLocation in Google maps geometry library.

I am trying to use the containsLocation from the Google geometry library, but can't get it to work...

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

The Javascript console gives an error, but that points to a function in Google's lib. So I assume the problem should lie somewhere in this function.

like image 490
Neograph734 Avatar asked May 31 '12 15:05

Neograph734


2 Answers

Oke, stupid me

I was wrong using all the coords as an array, I had to use the created polygon object.

var polyOptions = {
 ...
}

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

if(google.maps.geometry.poly.containsLocation(point, draw) == true) {
 alert("yes");
}
like image 183
Neograph734 Avatar answered Sep 24 '22 02:09

Neograph734


i had the same problem ([object Object]), I could solve that creating the polygon variable like that: draw = new google.maps.Polygon({paths:polyOptions});

Then the problem disappeared.

like image 39
user3037320 Avatar answered Sep 25 '22 02:09

user3037320