2015-10-14 48 views
0

我必須從我的Geoserver加載一個WFS圖層到我的網站Openlayers 3.9.0。矢量源URL和加載器之間的區別 - openlayers 3

根據the manual有兩種選擇來加載功能,loaderol.FeatureLoader)和urlol.FeatureUrlFunction)。

我沒有得到兩者之間的區別。他們都用來加載功能,loader,沒有設置任何URL,看起來更復雜。

我嘗試

var url = 'http://localhost:8080/geoserver/mapname/wfs?service=WFS&'+'version=1.0.0&request=GetFeature&typeName=mapname:awesomelayer&'+'outputFormat=application/json&maxFeatures=50'  
var vectorSource = new ol.source.Vector({ 
      format: new ol.format.GeoJSON(), 
      loader: function(extent){ 
       $.ajax({ 
       url: url, 
       type:'GET', 
       dataType: 'jsonp' 
       }).done(loadFeatures); 
      }, 
      strategy: new ol.loadingstrategy.tile(ol.tilegrid.createXYZ({maxZoom: 20})) 
}); 

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

,並在所有沒有錯誤,也沒有特色。

然後,我只是設置好的

var url = 'http://localhost:8080/geoserver/mapname/wfs?service=WFS&'+'version=1.0.0&request=GetFeature&typeName=mapname:awesomelayer&'+'outputFormat=application/json&maxFeatures=50'  
var vectorSource = new ol.source.Vector({ 
      format: new ol.format.GeoJSON(), 
      strategy: new ol.loadingstrategy.tile(ol.tilegrid.createXYZ({maxZoom: 20})), 
      url: function(extent, resolution, projection){return url} 
}); 

和工作。

我沒有得到區別,url是simler,更快,並且不需要loadFeatures函數。我把這本手冊弄紅了,但實際上,在代碼方面,我無法理解它。什麼是loader,爲什麼它沒有設置網址以及何時使用它?我在這裏錯過了什麼?

謝謝

回答

1

第一個想到的原因是:要發現錯誤。如果您的服務器能夠捕獲錯誤並將其返回到響應中,那麼您可能需要在加載器中讀取它們並採取相應措施。

第二個原因是在將這些功能添加到源之前計算/執行某些操作。這是一個example in an other thread from StackOverflow。它具有使用裝載器的兩個原因。

如果您在添加數據之前不需要對數據做任何特殊處理,或者您不需要管理錯誤,那麼url就足夠滿足您的要求了。

+0

好,現在有道理。順便說一下,關於爲什麼加載程序版本不起作用的任何想法?再次感謝 – user2860857

+1

嗨。如果你可以創建一個JSFiddle,我很樂意幫助你查看哪些問題。 –

+0

檢查[this](http://jsfiddle.net/slevin/cn8jsdmz/1/)。我不知道爲什麼它不起作用。我使用'jQuery 1.11.3'而不是'1.11.0'。如果我用'http:// localhost:3000/geoserver/mymap/wfs'替換'http:// demo.opengeo.org/geoserver/wfs',即使看不到任何錯誤,它也不起作用。 'wfs GET'的狀態爲'200 OK'。我沒有明白,對我來說看起來很好,我可以發現錯誤。任何提示都會很棒。謝謝 – user2860857