I am using the nowCOAST Map Server to display the recent Weather Radar image. nowcoast/radar_meteo_imagery_nexrad_time (MapServer) I'm having trouble with adding the corresponding Legend?
Wendi,
Here is a sample using 4.1:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>View padding - 4.1</title>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
#sidebar {
z-index: 99;
position: absolute;
top: 0;
right: 0;
height: 100%;
background: rgba(0, 0, 0, 0.5);
width: 320px;
overflow-y: auto;
}
#text {
color: white;
padding: 3%;
}
.esri-legend.esri-widget {
background-color: rgba(255,255,255,0);
}
</style>
<link rel="stylesheet" href="https://js.arcgis.com/4.1/esri/css/main.css">
<script src="https://js.arcgis.com/4.1/"></script>
<script>
require([
"esri/Map",
"esri/views/MapView",
"esri/widgets/Legend",
"dojo/dom",
"esri/layers/MapImageLayer",
"esri/renderers/SimpleRenderer",
"esri/symbols/SimpleLineSymbol",
"dojo/domReady!"
], function(
Map,
MapView,
Legend,
dom,
MapImageLayer,
SimpleRenderer,
SimpleLineSymbol
) {
var renderer = new SimpleRenderer({
symbol: new SimpleLineSymbol({
color: [255, 255, 255, 0.5],
width: 0.75,
style: "long-dash-dot-dot"
})
});
var layer = new MapImageLayer({
url: "https://nowcoast.noaa.gov/arcgis/rest/services/nowcoast/radar_meteo_imagery_nexrad_time/MapServer"
});
// Create the Map
var map = new Map({
basemap: "topo",
layers: [layer]
});
// Create the view set the view padding to be 320 px from the right
var view = new MapView({
container: "viewDiv",
map: map,
center: [-74.045459, 40.690083], // Liberty Island, NY, USA
zoom: 16,
padding: {
right: 320 // Same value as the #sidebar width in CSS
}
});
view.then(function() {
var legend = new Legend({
view: view,
layerInfos: [{
layer: layer,
title: "nowcoast/radar meteo imagery nexrad time"
}]
}, "text");
});
});
</script>
</head>
<body>
<div id="viewDiv">
<div id="sidebar">
<div id="text">
</div>
</div>
</div>
</body>
</html>
Thank you Robert! I am using 3.9 but will look into updating to 4.1. Also, I was using "DynamicMapServiceLayer" which probably isn't necessary.
Wendi,
It is hard to guess now a days which version people are using. Here is a sample for 3.18 then:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Map with legend</title>
<link rel="stylesheet" href="https://js.arcgis.com/3.18/dijit/themes/claro/claro.css">
<link rel="stylesheet" href="https://js.arcgis.com/3.18/esri/css/esri.css">
<style>
html, body {
height: 97%;
width: 98%;
margin: 1%;
}
#rightPane {
width: 20%;
}
#legendPane {
border: solid #97DCF2 1px;
}
</style>
<script src="https://js.arcgis.com/3.18/"></script>
<script>
var map;
require([
"esri/map", "esri/layers/ArcGISDynamicMapServiceLayer", "esri/dijit/Legend",
"dojo/_base/array", "dojo/parser",
"dijit/layout/BorderContainer", "dijit/layout/ContentPane",
"dijit/layout/AccordionContainer", "dojo/domReady!"
], function(
Map, ArcGISDynamicMapServiceLayer, Legend,
arrayUtils, parser
) {
parser.parse();
map = new Map("map", {
basemap:"topo",
center: [-96.53, 38.374],
zoom: 4
});
var radar = new ArcGISDynamicMapServiceLayer("https://nowcoast.noaa.gov/arcgis/rest/services/nowcoast/radar_meteo_imagery_nexrad_time/MapServer");
//add the legend
map.on("layers-add-result", function (evt) {
var layerInfo = arrayUtils.map(evt.layers, function (layer, index) {
return {layer:layer.layer, title:layer.layer.name};
});
if (layerInfo.length > 0) {
var legendDijit = new Legend({
map: map,
layerInfos: layerInfo
}, "legendDiv");
legendDijit.startup();
}
});
map.addLayers([radar]);
});
</script>
</head>
<body class="claro">
<div id="content"
data-dojo-type="dijit/layout/BorderContainer"
data-dojo-props="design:'headline', gutters:true"
style="width: 100%; height: 100%; margin: 0;">
<div id="rightPane"
data-dojo-type="dijit/layout/ContentPane"
data-dojo-props="region:'right'">
<div data-dojo-type="dijit/layout/AccordionContainer">
<div data-dojo-type="dijit/layout/ContentPane" id="legendPane"
data-dojo-props="title:'Legend', selected:true">
<div id="legendDiv"></div>
</div>
<div data-dojo-type="dijit/layout/ContentPane"
data-dojo-props="title:'Pane 2'">
This pane could contain tools or additional content
</div>
</div>
</div>
<div id="map"
data-dojo-type="dijit/layout/ContentPane"
data-dojo-props="region:'center'"
style="overflow:hidden;">
</div>
</div>
</body>
</html>
Don't forget to mark this question as answered by clicking on the "Correct Answer" link on the reply that answered your question.