[OpenLayers-Users] Problems displaying kmz file in OpenLayers

Andreas Hocevar ahocevar at opengeo.org
Fri Mar 27 16:44:24 EDT 2009


Hi,

On Thu, Mar 26, 2009 at 5:08 PM, Asle Benoni <asle.benoni at gmail.com> wrote:
> I am really frustrated about not beeing able to display a kmz layer in
> OpenLayers.
> Here is the code:
>
>                 map.addLayer(new OpenLayers.Layer.GML("Gravminne", "http://naturkart.no:8080/geoserver/wms/kml?layers=kulturminner_ns:gravminne&format=application/vnd.google-earth.kml+XML&transparent=true&srs=EPSG:4326&transparent=true&
> ",
>                {
>                 format: OpenLayers.Format.KML,
>                 formatOptions: {
>                   extractStyles: true,
>                   extractAttributes: true,
>                   transparent:true
>                 }
>                }));

The geoserver url you are using here will always return KMZ, which
OpenLayers cannot parse. Instead, you should use the following to
create your layer:

map.addLayer(new OpenLayers.Layer.WFS("Gravminne",
"http://naturkart.no:8080/geoserver/wms?service=WMS&request=GetMap&layers=kulturminner_ns:gravminne&format=kml&srs=EPSG:4326",
                {
                 format: OpenLayers.Format.KML,
                 formatOptions: {
                   extractStyles: true,
                   extractAttributes: true
                 }
                }));

Is there a specific reason why you  want to use KML? WFS would give
you the same result, you would only have to use a style map for your
symbology in addition. This is how to add the layer using the new WFS
protocol:

map.addLayer(new OpenLayers.Layer.Vector("Gravminne", {
    strategies: [new OpenLayers.Strategy.BBOX()],
    protocol: new OpenLayers.Protocol.WFS({
        url: "http://naturkart.no:8080/geoserver/wfs",
        typeName: "gravminne",
        featurePrefix: "kulturminner_ns"
    })
));

For the symbology, you can use OpenLayers.Format.SLD to use a sld file
that you already have on your server. Look at examples/SLD.html to see
how to configure this.

And you need OpenLayers.ProxyHost configured with a working proxy url.

If a map image rendered on the server would also do, you could just
use a WMS layer, and don't even need a ProxyHost:

map.addLayer(new OpenLayers.Layer.WMS("Gravminne";
"http://naturkart.no:8080/geoserver/wms?", {
    transparent: true,
    layers: "kulturminner_ns:gravminne"
}));
}

> I can see the http request from the proxy so it is fetching correct
> data (I think) but I cannot see any on the map!
> The request looks like this (FireBug). I am not sure if the EPSG is
> correct. I also tried "EPSG:32632" which I set for the map.

With the WFS setup above (first of my code snippets), the data should
be automatically reprojected to the SRS of your map.

> Can anyone point to how to show a KMZ file from GeoServer as I need
> the options for that layer format. I checked the docs but did not get
> wiser.

As already said, OpenLayers can not parse KMZ, only KML. To make
GeoServer output KML, your request would have to be a WMS request and
look like this:

http://naturkart.no:8080/geoserver/wms/?service=wms&request=GetMap&layers=kulturminner_ns:gravminne&format=application/vnd.google-earth.kml+XML&format=kml&bbox=0,0,180,90&width=500&height=250&srs=EPSG:4326

This is what will get generated with the second of my code snippets above.

Regards,
Andreas.

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



More information about the Users mailing list