I
I
ivan-el2015-09-30 14:09:51
Yandex
ivan-el, 2015-09-30 14:09:51

Yandex.Maps (API v.2) - how to get data on regions added to the map?

Suppose regions have been added to the map, as follows:

ymaps.regions.load('RU', {
    lang: 'ru',
    quality: 1
}).then(function (result) {
    var regions = result.geoObjects;
    myMap.geoObjects.add(regions);
});

How do I then get this data by region (get to them), already OUTSIDE this function? In particular, we need to iterate over each region in a loop:
regions.each(function (reg) {
  if (reg.properties.get('name') == 'Какой-то регион') {
    myMap.setBounds(reg.geometry.getBounds());
    ...
  }
});

How can I get regions? I tried constructions like myMap.geoObjects.regions, with a finger to the sky.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2015-10-01
@healqq

The method is used to traverse the collection of GeoObjects
Inside the method, you can already check if the current object is a region and perform the desired action.
This is not very convenient if there are a lot of objects on the map.
An alternative would be to add all regions to a separate layer:
To do this, a new object of type geoObjectCollection is created, all regions are added to it.
After that, we simply call the each method and get only the regions. Method information:
geoObjectCollection yandex maps api

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question