var map; function init() { map = new esri.Map("map", { center: [-118.483, 34.025], zoom: 13 }); // various tile server names var mq = ["mtile01", "mtile02", "mtile03", "mtile04"]; var abc = ["a", "b", "c"];
// info used to create web tile layers var layers = [{ "options": { "id": "MapQuest", "visible": true, "tileServers": mq, "attribution": "�2012 MapQuest - Portions �2012 NAVTEQ, Intermap" }, "url": "http://${0}.mqcdn.com/tiles/1.0.0/vx/map/${1}/${2}/${3}.jpg" }, { "options": { "id": "OpenCycleMap", "visible": false, "tileServers": abc, "attribution": "<a href=\"http://www.opencyclemap.org/\">Map tiles from OpenCycleMap</a>"}, "url": "http://${0}.tile.opencyclemap.org/cycle/${1}/${2}/${3}.png" }, { "options": { "id": "Trulia Crime Heat Map", "visible": false, "attribution": "Heat map tiles from <a href=\"http://www.trulia.com/local/#crimes/los-angeles-ca\">Trulia</a>"}, "url": "http://tiles.trulia.com/tiles/crime_heatmap/${0}/${1}/${2}.png" }]; // clear out the side bar dojo.byId("leftPane").innerHTML = ""; // create and add the layers var layerDiv = dojo.create("div"); dojo.forEach(layers, function(l) { var lyr = new esri.layers.WebTileLayer(l.url, l.options); map.addLayer(lyr); // add a radio button for the layer dojo.create("input", { "type": "radio", "checked": l.options.visible, "name": "tiledLayers", "id": l.options.id, "value": l.options.id }, layerDiv); // then a label dojo.create("label", { "for": l.options.id, "innerHTML": l.options.id + "<br />" }, layerDiv); }); // add the radio buttons to the page dojo.place(layerDiv, dojo.byId("leftPane"));
// event delegation to toggle layers dojo.connect(dojo.byId("leftPane"), "onclick", toggleLayer); }
function toggleLayer(e) { console.log("clicked: ", e, dojo.byId(e.target.id)); if ( ! dojo.byId(e.target.id) ) { // return if we cannot find the DOM node for a layer return; } // hide other layers dojo.forEach(map.layerIds, function(id) { map.getLayer(id).hide(); }); // show the layer with the clicked radio button var layer = map.getLayer(e.target.id); layer.show();
// update the attribution console.log("layer copyright: ", layer.copyright); if ( layer.copyright ) { dojo.byId("attribution").innerHTML = layer.copyright; } } [/HTML]