Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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>

share|improve this question
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

4 Answers 4

up vote 6 down vote accepted

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')
share|improve this answer

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)
    },
share|improve this answer

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");
share|improve this answer
    
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

check out this:

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

i hope it helps you...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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