N
N
nurzhannogerbek2019-02-06 21:44:36
JavaScript
nurzhannogerbek, 2019-02-06 21:44:36

How to check whether a label is in polygons?

Hello comrades! Please help me figure it out.
There is a collection (GeoObjectCollection) in which polygons (Polygon) are stored. I'm trying to check if a Placemark is in a polygon. What is the correct way to do this in my particular case? I found the each
method in the documentation , which was supposed to iterate over all the elements of the collection. The below code doesn't work. What am I doing wrong?

self.polygonCollection.each(function (polygon) {
    if(polygon.contains(res.geometry.getCoordinates())) {
        console.log("Метка входит в полигон.")
    }
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nurzhannogerbek, 2019-02-07
@nurzhannogerbek

Found the problem. The correct code looks like this:

self.polygonCollection.each(function(polygon) {
    if (polygon.geometry.contains(res.geometry.getCoordinates())) {
        console.log("Метка входит в полигон.")
    }
 });

A
Arthur Mustafin, 2019-02-06
@virtual_hack2root

Try ditching jQuery and moving to forEach :

self.polygonCollection.forEach(function(polygon, i, arr) {
  if(polygon.contains(res.geometry.getCoordinates())) {
        console.log("Метка входит в полигон.")
    }
});

In jQuery :
$.each(self.polygonCollection, function (key, polygon) {
    if(polygon.contains(res.geometry.getCoordinates())) {
        console.log("Метка входит в полигон.")
    }
});

Better yet, avoid common variable names like polygon altogether, use something more compact or unique

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question