0

I have a map and I'd like to add features. The features are added correctly always at the end of the array. If there are more than 10 features added, the eleven one - is inserted anywhere.

draw.on('drawend', function(evt) {
    var feature = evt.feature;
    var anfang = ol.proj.transform(feature.getGeometry().getFirstCoordinate(), 'EPSG:3857', 'EPSG:4326');
    var ende = ol.proj.transform(feature.getGeometry().getLastCoordinate(), 'EPSG:3857', 'EPSG:4326');


        var url = '?lonlats='+anfang+'|'+ende+'&nogos=&profile=shortest&alternativeidx=0&format=geojson';
        url = encodeURIComponent(url);
        url = broutes+url;
        var response = new XMLHttpRequest();
        response.open('GET', url);
        var onError = function() {
            console.log('error');
        }

        response.onerror = onError;
        response.onload = function() {
            if (response.status == 200) {
                var data = Ext.decode(response.responseText);
                var arr = data.features[0].geometry.coordinates;
                var arrayNew = [];
                for (i=0;  i<arr.length; i++){
                    var n = ol.proj.transform(arr[i], 'EPSG:4326', 'EPSG:3857');
                    arrayNew.push(n);
                }
                var data = new ol.geom.LineString( arrayNew );
                //console.log(data);
                var featureSnake = new ol.Feature({
                        geometry: data
                });

                snakeSource.addFeature( featureSnake );
                var win = Ext.create('MyProject.view.window.Edit', {
                    record : featureSnake,
                    toDo: 'insert',
                });
                win.show();
            } else {
                onError();
            }
        }
E.Scott
  • 19
  • 3
  • See https://github.com/openlayers/openlayers/issues/5729 Either specify a features collect or use `spatialIndex: false` https://openlayers.org/en/v4.6.5/apidoc/ol.source.Vector.html#getFeaturesCollection – Mike Apr 08 '19 at 12:36

1 Answers1

0

I chanced my code like you told me (using version 4.3.3)

vectorSource = new ol.source.Vector({
    useSpatialIndex: false
});

Now I have the following result vectorSource.getFeatures()

polygon1
polygon2
...
polygon15
polygon2
polygon3
...
polygon15
polygon1

I have all features twice.

E.Scott
  • 19
  • 3
  • Do you log features as you add them? If your json returns features in an extent you might get the same results repeated in same cases. The documentation says `getFeatures()` returns features in a random order. With `useSpatialIndex: false` there should be a features collection so you could also try using `getFeaturesCollection().getArray()` – Mike Apr 09 '19 at 18:31
  • Thanks I solved the probelm with `getFeatureCollection().getArray()` – E.Scott Apr 11 '19 at 09:41