Ich hatte genau das gleiche Problem, und das ist, wie ich es gelöst:
Betrachten Sie die Vektorlader-Funktion Ihre GeoJSON-Dateien verarbeiten zu holen. Ich hatte zahlreiche Caching-Probleme, die keine Vektorladefunktion zum Abrufen von Geojson-Dateien verwenden, und die unten beschriebene Methode beseitigte dieses Problem. Zum Beispiel:
var utils = {
refreshGeoJson: function(source,url) {
var now = Date.now();
if (typeof url == 'undefined') {
url = source.getUrl();
}
url += '?t=' + now; //Add current time to prevent browser caching
console.info('refreshGeoJson url: ' + url);
this.getJson(url).when({
ready: function(response) {
var format = new ol.format.GeoJSON();
var features = format.readFeatures(response, {
featureProjection: 'EPSG:3857'
});
console.dir(features);
console.log(features.length);
source.addFeatures(features);
source.changed();
}
});
},
getJson: function(url) {
var xhr = new XMLHttpRequest(),
when = {},
onload = function() {
console.log('xhr.status onload() ' + xhr.status);
if (xhr.status === 200) {
console.log('getJson() xhr: ');
console.dir(xhr);
console.log('getJson() xhr.response: ');
console.dir(xhr.response);
when.ready.call(undefined, JSON.parse(xhr.response));
}
if (xhr.status === 404) {
console.log('file not found! url: ' + url);
alert("file not found!\n" + url);
}
},
onerror = function() {
console.info('Cannot XHR ' + JSON.stringify(url));
};
//console.log('getJson() - retrieve file url: ' + url);
xhr.open('GET', url, true);
xhr.setRequestHeader('cache-control', 'no-store');
xhr.onload = onload;
xhr.onerror = onerror;
xhr.send(null);
return {
when: function(obj) { when.ready = obj.ready; }
};
}
};
var vectorLoader = function(extent, resolution, projection) {
utils.refreshGeoJson(this);
}
var vectorSource = new ol.source.Vector({
url: /path-to/myFile.geojson',
format: new ol.format.GeoJSON({
defaultDataProjection :'EPSG:3857'
}),
loader: vectorLoader,
strategy: ol.loadingstrategy.all
});
Ich habe eine Timer-Funktion, die vectorSouce.clear()
aufruft, bevor die Daten für diese Schicht neu lädt. Durch Aufruf von .clear()
wird die Vektorladefunktion für die Kartenebene ausgelöst.
Können Sie uns einen Code von dem zeigen, was Sie versucht haben? – kaycee