dojo.require("dijit.dijit"); dojo.require("dijit.layout.BorderContainer"); dojo.require("dijit.layout.ContentPane"); dojo.require("dijit.layout.AccordionContainer"); dojo.require("esri.map"); dojo.require("esri.dijit.OverviewMap"); dojo.require("esri.dijit.Scalebar"); dojo.require("esri.dijit.BasemapGallery"); dojo.require("esri.layers.FeatureLayer"); dojo.require("esri.dijit.InfoWindow"); dojo.require("esri.dijit.Legend"); dojo.require("esri.arcgis.utils"); dojo.require("dijit.form.CheckBox"); var map; var legendLayers = []; function init() { //calculate the extent for the initial zoom var initExtent = new esri.geometry.Extent({"xmin":-15375461.113615725,"ymin":2593049.7475456917,"xmax":-6066042.564710014,"ymax":6413678.169350926,"spatialReference":{"wkid":102100}}); //code to create the map and add a basemap map = new esri.Map("mapDiv", { extent: initExtent } ); var basemapURL = "http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"; var basemap = new esri.layers.ArcGISTiledMapServiceLayer(basemapURL); map.addLayer(basemap); //resize the infowindow //map.infoWindow.resize(150,300); //infotemplate code creation here var pportContent = "<table><tr><td><b>Port Code:</b></td><td style='text-align:right'>${PORTNUM}</td></tr></table>"; // the code below can be included to pass a parameter to a web service, asp.net page, etc. // + "<a href='http://www.vanderbilt.edu/vector&portcode=${PORT}'>Click here to view historical data</a>"; var schedKContent = "<table><tr><td><b>Port Code:</b></td><td style='text-align:right'>${SCHEDK}</td></tr>" + "<tr><td><b>Country Name:</b></td><td style='text-align:right'>${CTRYNAME}</td></tr></table>"; //infotemplates for principal and schedule K ports var infotemplatePports = new esri.InfoTemplate("${PORTNAME}",pportContent); var schedKtemplatePorts = new esri.InfoTemplate("${FPRTNAME}",schedKContent); dojo.connect(map, 'onLoad', function (map) { var scalebar = new esri.dijit.Scalebar({ map: map }); //add the overview map overviewMapDijit = new esri.dijit.OverviewMap({ map: map }, dojo.byId('overviewMapDiv')); overviewMapDijit.startup(); createBasemapGallery(); }); var princPortLayer = new esri.layers.FeatureLayer("http://transp40.vuse.vanderbilt.edu/arcgis/rest/services/westhemgis/westhem/MapServer/3", { outFields: ["PORTNAME","PORTNUM"], infoTemplate: infotemplatePports }); legendLayers.push({ layer: princPortLayer, title: 'U.S. Ports' }); var schedKPortLayer = new esri.layers.FeatureLayer("http://transp40.vuse.vanderbilt.edu/arcgis/rest/services/westhemgis/westhem/MapServer/2", { outFields: ["SCHEDK","FPRTNAME","CTRYNAME"], infoTemplate: schedKtemplatePorts }); legendLayers.push({layer:schedKPortLayer, title:'Foreign Ports'}); map.addLayers([princPortLayer, schedKPortLayer]); dojo.connect(map,'onLayersAddResult',function(results){ var legend = new esri.dijit.Legend({ map: map, layerInfos: legendLayers.title }, "legendDiv"); legend.startup(); //add check boxes dojo.forEach(legendLayers,function(layer){ var layerName = layer.title; var checkBox = new dijit.form.CheckBox({ name: "checkBox" + layer.layer.id, value: layer.layer.id, checked: layer.layer.visible, onChange: function(evt) { var clayer = map.getLayer(this.value); clayer.setVisibility(!clayer.visible); this.checked = clayer.visible; } }); //add the check box and label to the toc dojo.place(checkBox.domNode,dojo.byId("toggle"),"after"); var checkLabel = dojo.create('label',{'for':checkBox.name, innerHTML:layerName},checkBox.domNode,"after"); dojo.place("<br />",checkLabel,"after"); }); }); dojo.connect(map, 'onLoad', function(theMap) { //resize the map when the browser resizes dojo.connect(dijit.byId('map'), 'resize', map,map.resize); }); } dojo.addOnLoad(init); function createBasemapGallery() { //add the basemap gallery to display maps from ArcGIS.com including bing maps var basemapGallery = new esri.dijit.BasemapGallery({ showArcGISBasemaps: true, map: map }, "basemapGalleryDiv"); basemapGallery.startup(); dojo.connect(basemapGallery, "onError", function (msg) { console.log(msg) }); };
var portseriesLayer = new esri.layers.FeatureLayer("http://transp40.vuse.vanderbilt.edu/arcgis/rest/services/westhemgis/westhem/MapServer/4", { outFields: ["NAV_UNIT_N","CITY_OR_TO","STATE_POST","OPERATORS","PURPOSE","COMMODITIE"], infoTemplate: infotemplatePortSeries, id: "Water Terminals" }); legendLayers.push({layer:portseriesLayer,title:"Water Terminals"}); //add legend icons dojo.connect(map,'onLayersAddResult',function(results){ var legend = new esri.dijit.Legend({ map: map, layerInfos: legendLayers }, "legendDiv"); legend.startup(); });
var legend = new esri.dijit.Legend({ map: map, layerInfos:[{layer:princPortLayer, title:"U.S. Ports"}, {layer:schedKPortLayer, title:"Foreign Ports"}] }, "legendDiv"); legend.startup();
after many years, your answer has helped me, thanks mister Jake.