A
A
Andrew Hecc2017-02-17 13:07:50
JavaScript
Andrew Hecc, 2017-02-17 13:07:50

How to make dynamic markers on Yandex Map?

Good afternoon.
In the project, we display company markers on a Yandex map and there are two parameters by which these same markers need to be configured - color and icon.
And everything seems to be fine, there are examples in the documentation , but these examples do not quite fit our case.
7caa166f1750422ba95e3852a65bbe88.png
The example with icons in labels works with a certain set of font icons from their library, but we have a set of our own icons. That is, you can completely connect your icon, as we are doing now, but then we lose the color or abandon the icons and make different colors.
How to achieve the dynamic customization we need?
Objects are entered through the ObjectManager because there are about 2-3 thousand of them.
Object example:

{
                    "type": "Feature",
                    "id": item.id,
                    "geometry": {
                        "type": "Point",
                        "coordinates": [Number(item.cards_data[0].value), Number(item.cards_data[1].value)]
                    },
                    "properties": {
                        "balloonContent": item.name,
                        "iconCaption": item.name,
                        regionId: item.region_id,
                        specificationId: specificationIds
                    },
                  // Сейчас мы выводим просто кастомную иконку вот таким вот образом
                    "options" :{
                     iconLayout: 'default#image',
                     iconImageHref: '/public/img/animals/' + icon +'.svg',
                     iconImageSize: [30, 42],
                     iconImageOffset: [-5, -38]
                    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2017-02-17
@Hecc

I did something like this:

// создаем шаблон
const IconTemplate = ymaps.templateLayoutFactory.createClass(
    '<div class="myIcon {% if (properties.isActive) %}isActive{% endif %}"></div>'
);

// создаем placemark с этим шаблоном
const mark = new ymaps.Placemark(
    coords,
    {isActive: true}, // этот объект доступен как properties в шаблоне
    {iconLayout: IconTemplate}
);

Well, in the template you can already do whatever you want.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question