@DmitryNs

Как заставить работать фильтр и группировку на яндекс картах?

Как заставить работать geoQuery и objectManager вместе? Сейчас у меня выводятся и группируются метки из json.

url: "/local/templates/main/components/bitrix/news.list/map_new/data.php",
      type: "POST",
      dataType: "json",
        }).done(function(response) {
         objectManager.add(response);
           console.log(response)


Добавил фильтр и теперь получаю метки так

jQuery.getJSON('/local/templates/main/components/bitrix/news.list/map_new/data.php', function (json) {
           window.myObjects = ymaps.geoQuery(json)
               .addToMap(myMap3)
       })


Но проблема в том, что первым методом все группируется, но не срабатывает фильтр. А вторым срабатывает фильтр, но не работает группировка. Можно ли их вообще как то совместить. Примеров на яндексе не нашел.

Полный код

<div>
    <div>
        <select id="city">
            <option selected="true" disabled="disabled">Город:</option>
            <option value="Москва">Москва</option>
            <option value="Пермь">Пермь</option>
        </select>
    </div>
    <div>
        <select id="location">
            <option selected="true" disabled="disabled">Улица:</option>
            <option value="Гагарина">Гагарина</option>
            <option value="Пушкина">Пушкина</option>
        </select>
    </div>
    <div>
        <select id="quantity">
            <option selected="true" disabled="disabled">Количество домов:</option>
            <option value="10">10</option>
            <option value="20">20</option>
            <option value="30">30</option>
            <option value="40">40</option>
        </select>
    </div>
</div>


<div id="map3" style="width: 600px; height: 400px"></div>

<script>

    ymaps.ready(init);

    function init () {
        var myMap3 = new ymaps.Map('map3', {
                center: [55.76, 37.64],
                zoom: 10
            }, {
                searchControlProvider: 'yandex#search'
            }),
            objectManager = new ymaps.ObjectManager({
                clusterize: true,
                gridSize: 32,
                clusterDisableClickZoom: true
            });



        objectManager.objects.options.set('preset', 'islands#greenDotIcon');
        objectManager.clusters.options.set('preset', 'islands#greenClusterIcons');
        myMap3.geoObjects.add(objectManager);

        jQuery.getJSON('/local/templates/main/components/bitrix/news.list/map_new/data.php', function (json) {
            window.myObjects = ymaps.geoQuery(json)
                .addToMap(myMap3)
        })



        function checkState () {
            var shownObjects,
                quantity=$('#quantity').val(),
                city=$('#city').val(),
                location=$('#location').val(),
                filter_c=new ymaps.GeoQueryResult(),
                filter_l=new ymaps.GeoQueryResult(),
                filter_q=new ymaps.GeoQueryResult();

            var variant=0;

            if(quantity!=null){
                variant+=1;
            }
            if(city!=null){
                variant+=10;
            }
            if(location!=null){
                variant+=100;
            }

            switch(variant){
                case 1:
                    filter_q = myObjects.search('options.quantity="'+quantity+'"').add(filter_q);
                    shownObjects=filter_q.addToMap(myMap3);
                    break;
                case 10:
                    filter_c=myObjects.search('options.city="'+city+'"').add(filter_c);
                    shownObjects=filter_c.addToMap(myMap3);
                    break;
                case 100:
                    filter_l = myObjects.search('options.location="'+location+'"').add(filter_l);
                    shownObjects=filter_l.addToMap(myMap3);
                    break;
                case 11:
                    filter_q = myObjects.search('options.quantity="'+quantity+'"').add(filter_q);
                    filter_c=myObjects.search('options.city="'+city+'"').add(filter_c);
                    shownObjects=filter_c.intersect(filter_q).addToMap(myMap3);
                    break;
                case 101:
                    filter_q = myObjects.search('options.quantity="'+quantity+'"').add(filter_q);
                    filter_l = myObjects.search('options.location="'+location+'"').add(filter_l);
                    shownObjects=filter_l.intersect(filter_q).addToMap(myMap3);
                    break;
                case 110:
                    filter_l = myObjects.search('options.location="'+location+'"').add(filter_l);
                    filter_c=myObjects.search('options.city="'+city+'"').add(filter_c);
                    shownObjects=filter_c.intersect(filter_l).addToMap(myMap3);
                    break;
                case 111:
                    filter_q = myObjects.search('options.quantity="'+quantity+'"').add(filter_q);
                    filter_l = myObjects.search('options.location="'+location+'"').add(filter_l);
                    filter_c=myObjects.search('options.city="'+city+'"').add(filter_c);
                    shownObjects=filter_c.intersect(filter_l).intersect(filter_q).addToMap(myMap3);
                    break;
            }

            myObjects.remove(shownObjects).removeFromMap(myMap3);
        }

        $('#city').change(checkState);
        $('#location').change(checkState);
        $('#quantity').change(checkState);

    }

</script>
  • Вопрос задан
  • 181 просмотр
Пригласить эксперта
Ответы на вопрос 1
freeExec
@freeExec
Участник OpenStreetMap
1) удалить предыдущие из objectManager
2) отфильтровать через geoQuery
3) добавить полученное в objectManager
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
18 апр. 2024, в 21:56
2000 руб./за проект
18 апр. 2024, в 21:00
150 руб./за проект