Sunday 6 January 2019

Equivalent of layer.redraw(true) in OpenLayers 3?



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

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...