1

I have some vector layers that I import from geojson files and run some search on them, I want to display only the results of the search as a vector layer.

I am using a jquery form to search, and I get the result but it doesn't show on the map because the page is after that refreshed and the result is lost. I thought of exporting the result in a geojson file this way I can preserve it.

Here is the code for the search it works fine except that the result layer isn't added to the map.

jQuery('#default-search').submit(function(event) {

    var Results=[];
    var src=layer.getSource();
    src.forEachFeature(function(feature) {
        if(feature.get('Name')==='some_Name'){
            Results.push(feature);
        }
    });     
    tmp_source=new ol.source.Vector({
                features: Results
                });
    tmp=new ol.layer.Vector({
            title:'temporary layer', 
            source: tmp_source
            });

    map.addLayer(tmp);
    tmp.setVisible(true);

});

Is this possible or is there another way easier ?

  • 1
    try to add event.preventDefault(); as first statement in the function, by this you should be able to prevent the reloading of the page. – werkuh Mar 16 '16 at 14:50
2

I agree with Hicham (above). Your code seems fine (hard to tell without an example of the results returned) but refreshing of a page on a submit suggests a mishandling of the submit event. (Plenty of Stack Overflow discussion on the topic, here is one I like).

You might try instead attaching an event handler to the submit function that specifically triggers the function where you do something with the results:

$(document).on('keypress', '#default-search', function(event) {
  event.preventDefault();
  if(event.which == 13) { //trigger something with the 'enter' key
    //do something
  }
});
0

After a long detour to get a solution for this I finally got to the conclusion that we can't write in a file with javascript, and to do so I had to use php with AJAX

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

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.