1

I'm using OpenLayers to add a WFS layer. It works fine when i add it to init() function. But when i add the code to a function that is called by a button, layer gets added, but it does not display. It only displays if i uncheck and recheck the layer in the LayerSwitcher control.

I tried different things like the display and drawFeature funtions but they didn't work for me. IE:

wfs.display(true); 

What can i do to Add AND DISPLAY my wfs layer?

here is my test code:

<html xmlns="http://www.w3.org/1999/xhtml"> 
  <head> 
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" /> 
    <meta name="apple-mobile-web-app-capable" content="yes" /> 
    <link rel="stylesheet" href="../js/OpenLayers/theme/default/style.css" type="text/css" /> 
    <link rel="stylesheet" href="style.css" type="text/css" /> 
    <title>WFS: GetFeature Example (GeoServer)</title> 
    <script src="../js/OpenLayers/lib/OpenLayers.js" type="text/javascript"></script> 
    <script type="text/javascript">
        var map, layer, select, hover, control;
        OpenLayers.ProxyHost = "../secure/proxy/proxy.ashx?url=";
        var lon = 0;
        var lat = 0;
        var zoom = 0;
        function AddResultsLayer() {

            var wfs = new OpenLayers.Layer.Vector("WFS", {
                strategies: [new OpenLayers.Strategy.BBOX()],
                protocol: new OpenLayers.Protocol.WFS({
                    url: "http://demo.opengeo.org/geoserver/wfs",
                    featureType: "tasmania_roads",
                    featureNS: "http://www.openplans.org/topp"
                }),
                styleMap: new OpenLayers.StyleMap({
                    strokeWidth: 3,
                    strokeColor: "#333333"
                })
            })
            map.addLayer(wfs);
        }

        function init() {

            map = new OpenLayers.Map({
                div: "map",
                center: new OpenLayers.LonLat(0, 0),
                minResolution: "auto",
                minExtent: new OpenLayers.Bounds(-1, -1, 1, 1),
                maxResolution: "auto",
                maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90)
            });

            var basemap = 
                new OpenLayers.Layer.WMS(
                    "Natural Earth",
                    "http://demo.opengeo.org/geoserver/wms",
                    { layers: "topp:naturalearth" }
                )


            map.addControl(new OpenLayers.Control.LayerSwitcher());

            map.addLayer(basemap);
            map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);

        }
    </script> 
  </head> 
  <body onload="init()"> 
<h1 id="title">WFS Test</h1> 

<div id="tags"> 
WFS, GetFeature
</div> 

<p id="shortdesc"> 
    Shows how to use the GetFeature control to select features from a WMS layer.
</p> 

<div id="map" class="smallmap"></div> 

<div id="docs"> 
   <input id="cmdAddLayer" type="button" value="button" onclick="AddResultsLayer(); return false;" />
</div> 
  </body> 
</html> 

1 Answer 1

3

Well it's working now. I guess you need to call the refresh operation like so:

function AddResultsLayer() {

    var wfs = new OpenLayers.Layer.Vector("WFS", {
        strategies: [new OpenLayers.Strategy.BBOX()],
        protocol: new OpenLayers.Protocol.WFS({
            url: "http://demo.opengeo.org/geoserver/wfs",
            featureType: "tasmania_roads",
            featureNS: "http://www.openplans.org/topp"
        }),
        styleMap: new OpenLayers.StyleMap({
            strokeWidth: 3,
            strokeColor: "#333333"
        })
    })
    map.addLayer(wfs);
    wfs.refresh();

}

Key line being:

wfs.refresh();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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