I add from an array with addresses to the map tags

var addresses = [ 'Санкт-Петербург, Проспект Стачек, 100', 'Санкт-Петербург, Проспект Славы, 23' ]; /*--------------*/ ymaps.ready(init); function init(){ var map = new ymaps.Map('map', { center: [59.93, 30.33], zoom: 12, behaviors: ['default', 'scrollZoom'], controls: ['mapTools'], }); function addLabel(map, addr) { var geocoder = new ymaps.geocode( addr, { results: 1 } ); geocoder.then(function (res) { map.geoObjects.add(res.geoObjects.get(0)); }); } for (var i=0; i<addresses.length; ++i) { addLabel(map, addresses[i]); } } 

It is necessary, depending on the area to paint them in a different color. Or at least the polygons on the districts themselves do How to determine the area at? http://jsfiddle.net/pw4Tc/97/

    1 answer 1

    Determination of the area through the API is possible only through reverse geocoding by coordinates. When requesting a geocoder, you need to pass the coordinates of the point and the option kind with the value of district , in this case, the geocoder will return the name of the area, knowing that you can paint the labels in some color from the specified set.

    To determine the area of ​​the city in your case, you can:

     function addLabel(map, addr) { ymaps.geocode(addr, { results: 1 }) .then(function (res) { var point = res.geoObjects.get(0); var coordinates = point.geometry.getCoordinates(); ymaps.geocode(coordinates, { kind: 'district' }) .then(function (res) { var district = res.geoObjects.get(0) // Название района города district.properties.get('name'); // Добавление точки на карту map.geoObjects.add(point); }); }); } 
    • He defines municipal districts, and I need districts, Kirovsky, Kalininsky, etc.) - zkolya
    • @zkolya, yes, indeed, but information on areas is still there, for example, you can use the description property, district.properties.get('description') . - Maxim Zasorin
    • Does it make it difficult to suggest that I’m stuck, how to pass this value and use, for example, in a loop? Outside the function. - zkolya
    • @zkolya, pass, for example, a callback function to the addLabel function, and when the name of the district becomes known, call this callback and transfer the name of the district to it, or use Promise. - Maxim Zasorin