2

I am using OL3 and GeoServer and PostGIS as database. My database is connected to GeoServer and OL3 conected to GeoServer and reads the points in database as vector layer and shows on the map.

My data are changed every 5 seconds (position of points). I want to refresh the map automatically each 5 seconds to show the movement of the points.

Is there any way to do this?

EDIT: I am using WFS like this:

var vectorSource = new ol.source.ServerVector({
    format: new ol.format.GeoJSON(),       
    loader: function(extent, resolution, projection) {
        var url = 'http://localhost:8000/geoserver/cite/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=cite:MyFeature&maxFeatures=50&outputFormat=application/json';
        $.ajax({
            url: url
        })
        .done(loadFeatures);
    }
});

I can use window.setTimeout(function () { location.reload() }, 5000); in the loadFeatures function to reload the page each 5 seconds. It is working but I am searching for a better way which refreshes the features and not the complete map.

  • How are you loading the data into the map. WFS, JSON, WKT? It is reasonably straightforward to make loops in Javascript on a timer, but more information would be useful. – John Powell Nov 29 '14 at 18:19
  • @JohnBarça I provided information. – mike Nov 29 '14 at 19:26
2

The problem with the window.setTimeout function is it reloads the whole page. If you want to reload only the layer data you can use this :

var timer = null;
var vectorSource = new ol.source.ServerVector({
    format: new ol.format.GeoJSON(),       
    loader: function(extent, resolution, projection) {
        if (timer != null) {
            clearInterval(timer);
            timer = null;
        }
        var url = 'http://localhost:8000/geoserver/cite/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=cite:MyFeature&maxFeatures=50&outputFormat=application/json';
        $.ajax({
            url: url
        })
        .done(loadFeatures);
    }
});

at the end of your loadFeatures function add:

if (timer == null) {
    timer = setInterval(function() {
        vectorSource.clear();
    }, 500);
}

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.