2

I am getting this error in vector source when features are specified. The features are getting successfully and the array contains the features

var features = [];

$.ajax({
    url: 'geoJSON-url',
    dataType: 'json',
    async: false,
    success: function(json1) {
        $.each(json1, function (key, data) {
            if (key == 'features') {
                $.each(data, function (k, v) {
                    if (v.type=='Feature') {
                            features[k] = v;
                    }
                });
            }
        });
    }
});
    var source = new VectorSource({
      format: new GeoJSON(),
      features: features
    });

1 Answer 1

5

Your AJAX success function is creating an array of GeoJSON features. OpenLayers expects OpenLayers features.

$.ajax({
    url: 'geoJSON-url',
    dataType: 'json',
    async: false,
    success: function(json1) {
        features = new GeoJSON().readFeatures(json1);
    }
});

It would be simpler to let OpenLayers asychronously load and parse the URL

var source = new VectorSource({
  format: new GeoJSON(),
  url: 'geoJSON-url'
});
2
  • Actually, I tried to create cluster , the features is displayed on the map, but if I use this source to create a cluster source, I get an error - cannot read getType property with null. Looking for a solution found a solution using ajax so i tried with it. openlayers.org/en/latest/examples/cluster.html
    – Robotoflex
    Commented Jun 21, 2021 at 10:13
  • This is my geoJSON file is bad for a cluster. Thanks for answer
    – Robotoflex
    Commented Jun 21, 2021 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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