[OpenLayers-Users] Having Trouble with displaying features

Andreas Hocevar ahocevar at opengeo.org
Mon Mar 16 08:24:40 EDT 2009


Hi,

On Tue, Mar 10, 2009 at 3:48 AM, George Silva <georger.silva at gmail.com> wrote:
> I'm quite a novice OL user. I'm using Geoserver with PostGIS layers, and
> i would like to display them with OL. In the "Demo" session of GeoServer
> admin page i can see my FeatureType and GeoServer correctly renders a
> test page of this FeatureType for me.
>
> However, when i'm writing my own code, the map gets displayed, but its
> not using the simbology i selected with GeoServer. Here is the code:
>
> *<script
> src="http://localhost:8080/geoserver/openlayers/OpenLayers.js"></script>
>
>      <script type="text/javascript">
>
>      var map;
>
>      function init() {
>      map = new OpenLayers.Map('map');
>
>      var log = new OpenLayers.Layer.WFS(
>      "Logradouros",
>      "http://localhost:8080/geoserver/wfs",
>      {typename: "sigma:logradouros"}
>      );
>
>      map.addLayers([log]);
>      map.zoomToMaxExtent();
>      }
>      </script>
>
> *My question is: is there anything wrong with this code? I wrote it
> based on examples found on the web such as:
> http://workshops.opengeo.org/openlayers/intro/doc/en/layers.html

Your code does something completely different than the GeoServer demo.
Your code loads the geometries and renders them on the client, whereas
the GeoServer demo renders the geometries on the server and sends them
to the client as images.

If you want to just display your geometries, you should look at the
code of the GeoServer demo to see how to achieve that. Basically it
involves using OpenLayers.Layer.WMS instead of OpenLayers.Layer.WMS.

If, on the other hand, you want to interact with your geometries (or
features, to be precise), you can use OpenLayers.Layer.WFS. Your code
is correct, but your features get rendered with the default style,
because you don't specify a StyleMap:

      var log = new OpenLayers.Layer.WFS(
      "Logradouros",
      "http://localhost:8080/geoserver/wfs",
      {typename: "sigma:logradouros",
       styleMap: new OpenLayers.StyleMap(...)}
      );

See http://docs.openlayers.org/library/feature_styling.html for
details. If you want to use the same style as on the server, you have
to parse the SLD file that you configured GeoServer with. You can see
how this work in one of our examples:
http://www.openlayers.org/dev/examples/sld.html

> Do i need to specify to OpenLayers exactly what simbology is going to be
> used? All i get is blank map.

Did you specify the correct extent? You can look at the GeoServer demo
code and copy/paste the section where the map bounds and the center
are set. This will at least make sure that your features get
displayed.

Regards,
Andreas.

-- 
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.



More information about the Users mailing list