Я был точно такой же вопрос, и это, как я ее решил:
Рассмотрим с помощью функции вектора загрузчика обрабатывать ваши выборки GeoJSON файлы. У меня было множество проблем с кешированием, не использующих функцию векторной загрузки, чтобы извлекать файлы geojson, а метод ниже устраняет эту проблему. Например:
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
});
У меня есть приуроченная функция, которая вызывает vectorSouce.clear()
прежде чем он перезагружает данные для этого слоя. Вызов .clear()
запускает функцию векторного загрузчика для слоя карты.
Можете ли вы показать нам какой-то код того, что вы пробовали? – kaycee