2016-11-08 5 views
-1

GEOJSONファイルからレイヤーを作成します。私はファイルにいくつかの変更を加え、変更がマップに反映されることを願っています。しかし、地図は同じままです。GeoJsonファイルの変更がマップ上で更新されない

私は地図に強制的にJSONファイルをリロードする必要がありますが、それを行う方法を疑問に思います。

+0

試したことのあるコードを表示できますか? – kaycee

答えて

0

私は正確に同じ問題を持っていたし、これは私がそれを解決する方法である:

はあなたに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()を呼び出すと、マップ画層のベクトルローダー機能が起動されます。

関連する問題