Search code examples
javascriptopenlayers-3guide4you

GUIDE4YOU - how to add a dynamic layer?


The guys from guide4you did a great job making this lib opensource!!

I've succeeded in having a working demo guide4you sample.

How adjustable is the lib? For instance how can I add layers with GeoJSON instead of KML. Can the layers be added dynamically (with own javascript) instead of predefined?

Take this as example: https://openlayers.org/en/latest/examples/geojson.html

To be more specific: how can that example work together with guide4you ?

Kind regards,

Sam


Solution

  • To use a GeoJSON layer in guide4you you can just specify the type "GeoJSON" in the layerconfig.

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

    See also https://github.com/KlausBenndorf/guide4you/blob/master/conf/full/layers.commented.json for some examples

    If you like to add a layer on the fly with javascript you can use this api function:

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

    the possible options are the same as in the layer config.

    If you like to add only new features you can create a layer with type "Intern" and add the features with openlayers functionalities. The source of a feature layer is a subclass of ol.source.Vector. In the example below I am assuming that geojsonObject is of the same kind as in the geojson example of openlayers. var layer = map.get('api').addFeatureLayer({ "id": "3", "type": "Intern", "source": { "features": [] }, "visible": true }); layer.getSource().addFeatures((new ol.format.GeoJSON()).readFeatures(geojsonObject));

    And last but not least you can use a simplified api to define features inside a layerConfig object like this:

    { "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 ..." }] } }

    this can be used either in a layerConfig file or in the addFeatureLayer api method.