3

I'm adding a Stamen basemap to OpenLayers using the syntax below. Is it possible to change the layer's name, so that it appears in the Layer Switcher using this name (rather than "toner")?

The OpenLayers layer.setName method doesn't seem to work.

Thanks

<html>
<head>
    <title>tile.stamen.com: OpenLayers</title>
    <script type="text/javascript" src="http://www.openlayers.org/api/OpenLayers.js"></script>
    <script type="text/javascript" src="http://maps.stamen.com/js/tile.stamen.js?v1.1.2"></script>
    <script type="text/javascript">
        var map, layer;
        function initialize() {
            map = new OpenLayers.Map('map', {controls: [new OpenLayers.Control.LayerSwitcher({})]});
            stamenLayer = new OpenLayers.Layer.Stamen("toner");
            stamenLayer.setName = "custom layer name";
            map.addLayer(stamenLayer);
            map.setCenter(new OpenLayers.LonLat(-13630355, 4546576), 10);

        }
    </script>
    <style type="text/css">
        #map {
            width: 800px;
            height: 600px;
        }
    </style>
</head>
<body onload="initialize()">
    <div id="map"></div>
</body>

  • 2
    setName is a method...you need to try stamenLayer.setName("custom layer name"); – ca0v Aug 27 '12 at 13:44
  • @ca0v thanks - you should have put that as an answer cos you're right! – Stephen Lead Aug 27 '12 at 22:14
6

Try to change the name of the layer after adding the layer.

Here it works: http://maps.stamen.com/test/openlayers.html

write in console:

 map.addControl(new OpenLayers.Control.LayerSwitcher)

then

layer.setName('customName')
3

Looking at the source for the Stamen layer it seems the name is used to determine the provider:

OpenLayers.Layer.Stamen = OpenLayers.Class(OpenLayers.Layer.OSM, {
    initialize: function(name, options) {
        var provider = getProvider(name),
            url = provider.url

And finally the OSM constructor is invoked:

        return OpenLayers.Layer.OSM.prototype.initialize.call(this, name, hosts, options);

So it does seem that setName is the better option since you need the first parameter to match something in this PROVIDERS list:

PROVIDERS =  {
        "toner":        MAKE_PROVIDER("toner", "png", 0, 20),
        "terrain":      MAKE_PROVIDER("terrain", "jpg", 4, 18),
        "watercolor":   MAKE_PROVIDER("watercolor", "jpg", 3, 16)
    },
2

I've never used layer.setName before but if you want a custom name in the Layer Switcher:

stamenLayer = new OpenLayers.Layer.Stamen("whatever you type here 
                                           will show up in the layer switcher");
  • Thanks for the tip, however I don't believe this will actually work. As far as I can see, you need to use one of their supported keywords (toner, toner-lite, watercolor, etc) in order to specify a Stamen map – Stephen Lead Aug 27 '12 at 22:18
1

check out this:

map.layers[0].name = "newLayerName";
map.controls[4].redraw();  // OpenLayers.Control.LayerSwitcher

i hope it helps you...

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.