2
votes

I use the following block of JavaScript to try to show a WMS layer. I'm using OpenLayers 2.8.

The map's base layer (Openstreetmap) shows correctly, it zooms to the correct area, the "pyramid" layer is shown in the layer switcher, but no request to its WMS service is ever made (so the fact that the URL, styles and params are dummies shouldn't matter -- it never even attempts to get them).

OpenLayers does try to get a WMS layer once I pan or zoom far enough so that the Gulf of Guinea is in view (but all my data is in the Netherlands). This suggests a projection problem (WGS84's (0, 0) point is there), but I don't understand why OpenLayers doesn't even try to fetch a map layer elsewhere. My data is in EPSG:3857 (Web Mercator) projection.

/*global $, OpenLayers */
(function () {
    "use strict";

    $(function () {
        $(".map").each(function () {
            var div = $(this);

            var data_bounds = div.attr("data-bounds");
            console.log("data_bounds: " + data_bounds);

            if (data_bounds !== "") {
                var map = new OpenLayers.Map(div.attr("id"), {
                    projection: "EPSG:3857"});

                var extent = JSON.parse(data_bounds);
                var bounds = new OpenLayers.Bounds(
                    extent.minx, extent.miny,
                    extent.maxx, extent.maxy);

                map.addLayer(
                    new OpenLayers.Layer.OSM(
                        "OpenStreetMap NL",
                        "http://tile.openstreetmap.nl/tiles/${z}/${x}/${y}.png",
                        {buffer: 0}));

                map.addLayer(
                    new OpenLayers.Layer.WMS(
                        "pyramid", "http://rasterserver.local:5000/wms", {
                            layers: "test",
                            styles: "test"
                        }, {
                            singleTile: true,
                            isBaseLayer: false,
                            displayInLayerSwitcher: true,
                            units: 'm'
                        }));

                map.addControl(new OpenLayers.Control.LayerSwitcher());
                map.zoomToExtent(bounds);
            }
        });
    });
})();

Edit: the 'data_bounds' console print prints (with some added formatting):

data_bounds: {
    "minx": 582918.5701295201,
    "miny": 6923595.841021758,
    "maxx": 821926.9006116659,
    "maxy": 7079960.166533174
}

It zooms to the correct region in the north of the Netherlands, so I don't think the problem is there.

Since posting, I found out that if I don't use the OSM layer, and instead use the WMS layer as baselayer, it works. So perhaps there's some incompatibility with a OSM baselayer and a WMS layer added to it? But then I don't get that it does seem to do something near WGS84 (0, 0).

1
What are you bounds, or extent, as you have it in your code?John Powell
I added the bounds console output to the question.RemcoGerlich
Have you tried using 900913, the older designation of spherical mercator, instead of 3857, seeing as you are on OL 2.8?John Powell
I didn't, but a few seconds ago I managed to find an answer -- it works once I add a maxExtent to the map. No idea why. Thank you for your help!RemcoGerlich
Great. You haven't done mapping till you've loaded something in Nebraska and it appears in the sea of Ghana :-)John Powell

1 Answers

1
votes

I eventually managed to fix this by giving the map an explicit maxExtent:

var extent = JSON.parse(data_bounds);
var bounds = new OpenLayers.Bounds(
    extent.minx, extent.miny,
    extent.maxx, extent.maxy);

var map = new OpenLayers.Map(div.attr("id"), {
    projection: "EPSG:3857",
    maxExtent: bounds
});

Oddly enough this doesn't limit the user's ability to pan and zoom around the world, but it does make the overlay work...