2015-04-02 67 views
0

我正在嘗試使用OpenLayers3創建路線規劃應用程序,並且想知道是否有人有過類似的操作經驗? 我對此很新,但至今我設法將我的幾何圖形保存爲geoJson;我似乎無法檢索到它並在我的矢量圖層上重繪它。 理想情況下,我寧願使用本地化的數據庫解決方案,但我似乎無法找到任何引用此文檔。將矢量特徵寫入本地存儲/ IndexedDB並檢索

+0

IndexedDb是一個無模式對象存儲。一旦你創建了你的數據庫和對象存儲,你就可以開始存儲你的geoJSON對象。嘗試閱讀本文,看看你如何去http://www.codeproject.com/Articles/325135/Getting-Started-with-IndexedDB。有幾點需要注意,但它是異步的,如果你之前使用過javascript回調,它會有所幫助。 – 2015-04-02 18:11:35

+1

對於本地化數據庫,我使用[pouchdb](http://pouchdb.com)。它使得索引數據庫更容易使用,並添加了同步功能 – Alex 2015-04-03 07:38:17

+0

您是否記得在從本地存儲中檢索geojson之後,將其解析回對象?並將其保存在本地存儲之前將其串聯化?本地存儲包含字符串。看看[這](http://stackoverflow.com/questions/2010892/storing-objects-in-html5-本地存儲) – Alex 2015-04-03 07:45:37

回答

0

正如我在評論中所說的,感謝所有的輸入。解析並不是真正的問題,更多的是如何用檢索到的信息生成矢量圖層。 我偶然發現一篇文章,討論瞭如何爲檢索到的數據創建一個Loader函數,這與從外部文件或Web服務加載相反。相關的代碼將在下面進行復制。對我來說,PouchDB是最好的選擇,這要歸功於@Alex,因爲我已經開始閱讀它,這是我需要的所有確認。 ;)

// Load Route Function 
$(document).ready(function() { 
    if (localStorage.getItem('myFeatures') !== null) { 
    var features = JSON.parse(localStorage.getItem('myFeatures')); 
    console.log(features); 

    var featureSource = new ol.source.ServerVector({ 
    format: new ol.format.GeoJSON(), 
    loader: function(extent, resolution, projection) { 
     loadFeatures(); 
    }, 
    strategy: function(extent, resolution) { 
    // some code 
    return [extent]; 
    }, 
    projection: "EPSG:25832" 
}); 
} 

var loadFeatures = function() { 
    featureSource.addFeatures(featureSource.readFeatures(features)); 
}; 

var SmallworldLayer = new ol.layer.Vector({ 
    source: featureSource, 
    style: defaultRoute 
}); 
map.addLayer(SmallworldLayer); 
});