1

I am using PostGIS and GeoServer and OpenLayers 3 (OL3). I connected PostGIS table to GeoServer and published my layer. I am using following code to get the layer as vector layer:

var vector = new ol.layer.Vector({
    source: new ol.source.GeoJSON({
        projection: 'EPSG:4326',
        url: 'http://localhost:8000/geoserver/cite/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=cite:myfeaturesTbl&maxFeatures=50&outputFormat=application/json&'
    })
});

I need to read my feature as vector layer because I want to select them and then get information of each feature or move them on the map. When I use this code it is not show anything.

Is there any problem in my code?

Can you help me get a vector layer in OL3 with a GeoServer WFS?

  • Try the "Layer Preview" in GeoServer, and tell us if the layer works there. Also, is the client really running on the same machine (localhost) as GeoServer? – BradHards Nov 21 '14 at 10:35
  • Hello, Yes layer Preview shows the layer. also I can show the layer with WMS. also Geoserver and client in are in same machine. I wanna know how can I show a layer (vector layer), from geoserver with OL3? – mike Nov 21 '14 at 15:28
3

The Problem lies in your use of url, which in OL3 seems to expect only static files, i.e. url = "/static/data/geojson.json".

To fill your map with dynamically loaded data use ajax calls and the ol.source.Vector.addFeatures method:

var vectorSource = new ol.source.GeoJSON({
    projection: 'EPSG:4326'
}),

$.get(
    '/geoserver/cite/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=cite:myfeaturesTbl&maxFeatures=50&outputFormat=application/json'
    // success callback 
    function (data) {
        // first add features  
        vectorSource.addFeatures(vectorSource.readFeatures(data));

        // then display map...
        var map = new ol.Map({
                                target: '...',
                                layers: [
                                    new ol.layer.Tile({
                                        source: ...
                                    }),
                                    new ol.layer.Vector({  
                                        source: vectorSource 
                                    })
       });
   }
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.