I have a geojson layer in my OL3 app which I want to redraw every 5 seconds (to show movement on map) .
How do I do it ? Couldn't find the equivalent of Layer.redraw().
Answer
This is how you can refresh a vector source every 5 seconds, from a web service returning features in a GeoJSON document:
var vectorSource = new ol.source.Vector();
var geojsonFormat = new ol.format.GeoJSON();
window.setTimeout(function() {
$.ajax('http://example.com/data.json', function(data) {
var features = geojsonFormat.readFeatures(data
{featureProjection:"EPSG:3857"});
geojsonSource.clear();
geojsonSource.addFeatures(features);
});
}, 5000);
jQuery is used here for requesting the data through Ajax ($.ajax
), but you can obviously use the library of your choice.
This code snippet also assumes that the map's projections is "EPSG:3857" (web mercator) and that the coordinates in the GeoJSON documents are longitudes and latitudes.
No comments:
Post a Comment