K
K
Konstantin Savinov2015-08-02 21:00:06
JavaScript
Konstantin Savinov, 2015-08-02 21:00:06

How to load new tags in Yandex.Maps?

There are several sets of labels packaged in separate JSON files.
At the request of the user, it is necessary to display the set of labels he needs.
Right now I've implemented it with links on the page:

<a href="#" onclick="init();">Hey!</a>
<a href="#" onclick="init2();">Hey!</a>
<a href="#" onclick="init3();">Hey!</a>

On click, the following code is executed, each time generating a new map and deleting the old one:
function init () {
  $("ymaps").remove();
    var myMap = new ymaps.Map('map', {
            center: [55.76, 37.64],
            zoom: 10,
            controls: ['zoomControl']
        }, {
            searchControlProvider: 'yandex#search'
        }),
        objectManager = new ymaps.ObjectManager({
            clusterize: true,
            gridSize: 32
        });

    objectManager.objects.options.set('preset', 'islands#darkGreenCircleDotIcon');
    objectManager.clusters.options.set('preset', 'islands#invertedDarkGreenClusterIcons');
    myMap.geoObjects.add(objectManager);

    $.ajax({
        url: "1.json"
    }).done(function(data) {
        objectManager.add(data);
    });
}

Something tells me that there should be a more elegant way, in which it will not be necessary to call a new map every time, but you can simply change the labels on an existing one.
Am I right or wrong?
PS Solution found! Thanks for the advice!
ymaps.ready(init);
var myMap;

function init () {
    myMap = new ymaps.Map('map', {
            center: [55.76, 37.64],
            zoom: 11,
            controls: ['zoomControl']
        }, {
            searchControlProvider: 'yandex#search'
        }),
        objectManager = new ymaps.ObjectManager({
            clusterize: true,
            gridSize: 32
        });

    objectManager.objects.options.set('preset', 'islands#darkGreenCircleDotIcon');
    objectManager.clusters.options.set('preset', 'islands#invertedDarkGreenClusterIcons');
    myMap.geoObjects.add(objectManager);

    $.ajax({
        url: "1.json"
    }).done(function(data) {
        objectManager.add(data);
    });

}

function newpins () {
if(myMap)
myMap.geoObjects.removeAll();

objectManager = new ymaps.ObjectManager({
            clusterize: true,
            gridSize: 32
        });

objectManager.objects.options.set('preset', 'islands#darkGreenCircleDotIcon');
objectManager.clusters.options.set('preset', 'islands#invertedDarkGreenClusterIcons');
myMap.geoObjects.add(objectManager);

$.ajax({
url: "4.json"
}).done(function(data) {
objectManager.add(data);
});
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Barabanov, 2015-08-02
@Kruazzan

about

var myMap = new ymaps.Map('map', {
  center: [55.76, 37.64], 
  zoom: 10,
  controls: ['zoomControl']
}, {
   searchControlProvider: 'yandex#search'
});

function init () {
  if(myMap)
    myMap.geoObjects.removeAll();
  objectManager = new ymaps.ObjectManager({
    clusterize: true,
    gridSize: 32
  });
  objectManager.objects.options.set('preset', 'islands#darkGreenCircleDotIcon');
  objectManager.clusters.options.set('preset', 'islands#invertedDarkGreenClusterIcons');
  myMap.geoObjects.add(objectManager);

  $.ajax({
    url: "1.json"
  }).done(function(data) {
    objectManager.add(data);
  });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question