I have a vector layer in my OpenLayers applicaiton.

  var layer = new ol.layer.Vector({
      source: new ol.source.Vector({
          url: "my geojson url"

my GeoJSON data fills the vector layer.

I have an AJAX query that gets single feature from server. But I need to check my layer to control if feature is in layer.

     how to check if response feature in my layer?

Response is GeoJSON formatted data.


I'd use something like:

  features = vectorSource.getFeatures();

  found = false;
  for (i = 0; i < features.length && !found; i++) {
    if (features[i].getId() === id) {
      found = true;
      //Here I'm removing it, but you could do anything you like
      features.splice(i, 1);

You can easily do that by using the hasFeature() method of ol.source.Vector

var mySource = new ol.source.Vector({url: "geoJsonUrl"});
var myLayer = new ol.layer.Vector({source: mySource});

    var feature = the response feature
    //note the feature must be of type ol.Feature and i think it sholud have an id too
        //do logic here

check the doc at OpenLayers VectorSource hasFeature method

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.