1

I have some code in loop:

var vector =   new ol.layer.Vector({
                                title: name,
                                style: styleFunction
                            });
                             function getFeatures(geoJson) {
                                // If response is valid
                                var format = new ol.format.GeoJSON({
                                    featureProjection: "EPSG:3857"
                                });
                                vector.setSource(
                                        new ol.source.Vector({
                                            format: new ol.format.GeoJSON(),
                                            features: format.readFeatures(geoJson),

                                        }));
                            }

                            $.ajax({
                                type: "GET",
                                url: urlAjax,
                                dataType: 'jsonp',
                                jsonpCallback: 'getJson',
                                success:getFeatures
                            });

                            overlays.push(vector)

After push vector to overlay array I add that array of vectors to map:

 var map = new ol.Map({
        target: 'map',
        layers: [
            new ol.layer.Group({
                title: 'Base maps',
                layers: basemaps
            }),
            new ol.layer.Group({
                title: 'Overlays',
                layers: overlays
            })

        ],
        view: new ol.View({
            center: ol.proj.fromLonLat([31.4, 48.9]),
            zoom: 6
        })
    });

And create switcher controls... And all vectors are reset sources last layer and only last layer visible in map, others don`t work...why?

1

I found a solution. I had problem because I used ajax. Requests was asynchronized and source created asynchronized too... now I use this code to get sources:

 var vector = new ol.layer.Vector({
                        title: bean.name,
                        style: styleFunction,
                        source:new ol.source.Vector({
                                format:new ol.format.GeoJSON({
                                    featureProjection: "EPSG:3857"}),
                                url:bean.urlAjax
                        })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.