M
M
Maxim Ermakov2020-11-04 11:44:33
Yandex maps
Maxim Ermakov, 2020-11-04 11:44:33

Why is the object created by the function not deleted in Yandex.maps?

Hello.
I use something like this code (I cut it a little to the very essence, which I don’t get)

myMap.events.add('click', function (e) {
                               var coords = e.get('coords');

                               var iAmHerePlaceMark1 = new ymaps.Placemark(coords, {}, {
                                 preset: 'islands#geolocationIcon',draggable:true,
                               });

                                  myMap.geoObjects.remove(iAmHerePlaceMark1);
                                  window.myMap.geoObjects.add(iAmHerePlaceMark1);
                             
                        });

But the object created in the function is not deleted, although if I create an object outside the function, then it can safely be deleted from this function. geoObjects.remove() doesn't remove the function's local objects, that's what I'm talking about. How can I get them removed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Ermakov, 2020-11-04
@bossic

I figured it out myself. It should be something like this:

var myCollection = new ymaps.GeoObjectCollection();
                        myMap.events.add('click', function (e) {
                              myCollection.removeAll();
                          
                               var coords = e.get('coords');

                               var placeMark = new ymaps.Placemark(coords, {}, {
                                     preset: 'islands#geolocationIcon',draggable:true,
                               });
                               myCollection.add(placeMark);

                               myMap.geoObjects.add(myCollection);
                        });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question