2017-05-09 87 views
-1

來自guide4you的人做了很棒的工作,使這個lib開源!GUIDE4YOU - 如何添加動態圖層?

我已經成功地進行了工作演示guide4you sample。

lib如何調整? 例如,我怎樣才能用GeoJSON添加圖層而不是KML。 可以動態添加圖層(使用自己的javascript)而不是預定義的圖層嗎?

以此爲例子:https://openlayers.org/en/latest/examples/geojson.html

更具體地講:這怎麼可能例如與guide4you一起工作?

親切的問候,

山姆

回答

0

要guide4you你可以指定在layerconfig類型 「GeoJSON的」 使用GeoJSON的層。

{ "id": "3", "type": "GeoJSON", "source": { "url": "path/to/geojson" } }

https://github.com/KlausBenndorf/guide4you/blob/master/conf/full/layers.commented.json見的一些例子

如果你喜歡在飛行中使用JavaScript,你可以使用此API函數添加一個圖層:

map.get('api').addFeatureLayer({ "id": "3", "type": "GeoJSON", "source": { "url": "path/to/geojson" }, "visible": true })

的可能選項與圖層配置中的相同。

如果您只想添加新功能,您可以創建一個類型爲「Intern」的圖層,並添加具有openlayers功能的功能。要素圖層的來源是ol.source.Vector的子類。 在下面的示例中,我假定geojsonObject與openlayers的geojson示例中的類型相同。 var layer = map.get('api').addFeatureLayer({ "id": "3", "type": "Intern", "source": { "features": [] }, "visible": true }); layer.getSource().addFeatures((new ol.format.GeoJSON()).readFeatures(geojsonObject));

最後但並非最不重要的,你可以使用一個簡化的API定義layerConfig的物體,像這裏面的特點:

{ "id": "3", "type": "Intern", "source": { "features": [{ "id": 6, "name": "Some feature", "description: "Some description", "style": "#defaultStyle", "geometryWKT": "... any wkt string here ..." },{ "geometryWKT": "... any wkt string here ..." }] } }

這可以在layerConfig文件或addFeatureLayer API一起使用方法。

+0

嗨西蒙。感謝您的快速回答!當我將代碼放入腳本塊的index.html中時,我會得到「ReferenceError:map is not defined」 – Sam

+0

您可以使用createG4U('#g4u-map')。then(function(map){... here map is定義...}) –

+0

我自己試過了,它不起作用,因爲地圖在這一點上還沒有準備好。這應該工作:createG4U('#g4u-map')。然後(function(map){map.asSoonAs('ready',true,function(){... here ...})}) –