I have the following code and the script evaluate which layer is Support. In this case it is one. I would like to assign that value through JavaScript as opposed to hard coding it. jsonValue does give me what I want, but I can't figure out how to assign the number to the supportLayerUrl:
var jsonValue; // Get the id of a layer var requestHandle = esriRequest({ url: "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/", content: { f: 'json' }, handleAs: "json" }); requestHandle.then(function (lyrJSON, io) { for (var i = 0; i < lyrJSON.layers.length; i++) { if (lyrJSON.layers.name = "Support") { jsonValue = lyrJSON.layers.id; } } return jsonValue; }) function checkValue() { alert(jsonValue); } // app configuration var config = { mapOptions: { showAttribution: false, sliderStyle: "small", extent: initialExtent, logo: false, sliderPosition: "bottom-right" }, signLayerUrl: "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/0", supportLayerUrl: "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/1" };
I would like for my supportLayerUrl be something like this:
supportLayerUrl: "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/" + jsonValue
But I get jsonValue not defined. Any ideas?
Solved! Go to Solution.
Chris,
Here is simple sample that does this:
<!DOCTYPE html> <html> <head> <title>Get ArcGIS Server Map Service Layers</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" href="http://js.arcgis.com/3.13/esri/css/esri.css"> <style> body{ font-family: "Arial Unicode MS, Arial, sans-serif"; } #content { width: 800px; height: 350px; padding: 5px; overflow: auto; border: solid 2px #AAAAAA; background-color: #FFFFFF; -moz-border-radius: 5px; -webkit-border-radius: 5px; -o-border-radius: 5px; border-radius: 5px; -moz-box-shadow: 0 0 0.5em black; -webkit-box-shadow: 0 0 0.5em black; -o-box-shadow: 0 0 0.5em black; box-shadow: 0 0 0.5em black; } .failure { color: red; } #status { font-size: 12px; } </style> <script src="http://js.arcgis.com/3.13/"></script> <script> require(["dojo/dom", "dojo/on", "dojo/dom-class", "dojo/_base/json", "dojo/_base/array", "dojo/string", "esri/request", "dojo/_base/lang", "dojo/domReady!"], function(dom, on, domClass, dojoJson, array, dojoString, esriRequest, lang) { dom.byId("url").value = "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer"; dom.byId("content").value = ""; //handle the Go button's click event on(dom.byId("submitRequest"), "click", getContent); function getContent(){ var contentDiv = dom.byId("content"); contentDiv.value = ""; domClass.remove(contentDiv, "failure"); dom.byId("status").innerHTML = "Querying..."; //get the url and setup a proxy var url = dom.byId("url").value; if(url.length === 0){ alert("Please enter a URL"); return; } var requestHandle = esriRequest({ "url": url, "content": { "f": "json" }, "callbackParamName": "callback" }); requestHandle.then(requestSucceeded, requestFailed); } function requestSucceeded(response, io){ dom.byId("status").innerHTML = ""; var supportLyrId; console.info(response); array.some(response.layers, function(lyr){ if(lyr.name === "Support"){ supportLyrId = lyr.id; return true; } }); dom.byId("content").value = "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/" + supportLyrId; } function requestFailed(error, io){ domClass.add(dom.byId("content"), "failure"); dojoJson.toJsonIndentStr = " "; dom.byId("content").value = dojoJson.toJson(error, true); } }); </script> </head> <body> <p>Enter the URL for a layer in a map service to access metadata about using esriRequest.</p> <p> <input type="text" id="url" size="105"/> <input id="submitRequest" type="button" value="GO" /> <span id="status"></span> </p> <p> <h2>Content</h2> <p>Check the console for the full response. Here is the full support url:</p> <textarea id="content"></textarea> </p> </body> </html>
Chris,
Here is simple sample that does this:
<!DOCTYPE html> <html> <head> <title>Get ArcGIS Server Map Service Layers</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" href="http://js.arcgis.com/3.13/esri/css/esri.css"> <style> body{ font-family: "Arial Unicode MS, Arial, sans-serif"; } #content { width: 800px; height: 350px; padding: 5px; overflow: auto; border: solid 2px #AAAAAA; background-color: #FFFFFF; -moz-border-radius: 5px; -webkit-border-radius: 5px; -o-border-radius: 5px; border-radius: 5px; -moz-box-shadow: 0 0 0.5em black; -webkit-box-shadow: 0 0 0.5em black; -o-box-shadow: 0 0 0.5em black; box-shadow: 0 0 0.5em black; } .failure { color: red; } #status { font-size: 12px; } </style> <script src="http://js.arcgis.com/3.13/"></script> <script> require(["dojo/dom", "dojo/on", "dojo/dom-class", "dojo/_base/json", "dojo/_base/array", "dojo/string", "esri/request", "dojo/_base/lang", "dojo/domReady!"], function(dom, on, domClass, dojoJson, array, dojoString, esriRequest, lang) { dom.byId("url").value = "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer"; dom.byId("content").value = ""; //handle the Go button's click event on(dom.byId("submitRequest"), "click", getContent); function getContent(){ var contentDiv = dom.byId("content"); contentDiv.value = ""; domClass.remove(contentDiv, "failure"); dom.byId("status").innerHTML = "Querying..."; //get the url and setup a proxy var url = dom.byId("url").value; if(url.length === 0){ alert("Please enter a URL"); return; } var requestHandle = esriRequest({ "url": url, "content": { "f": "json" }, "callbackParamName": "callback" }); requestHandle.then(requestSucceeded, requestFailed); } function requestSucceeded(response, io){ dom.byId("status").innerHTML = ""; var supportLyrId; console.info(response); array.some(response.layers, function(lyr){ if(lyr.name === "Support"){ supportLyrId = lyr.id; return true; } }); dom.byId("content").value = "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/" + supportLyrId; } function requestFailed(error, io){ domClass.add(dom.byId("content"), "failure"); dojoJson.toJsonIndentStr = " "; dom.byId("content").value = dojoJson.toJson(error, true); } }); </script> </head> <body> <p>Enter the URL for a layer in a map service to access metadata about using esriRequest.</p> <p> <input type="text" id="url" size="105"/> <input id="submitRequest" type="button" value="GO" /> <span id="status"></span> </p> <p> <h2>Content</h2> <p>Check the console for the full response. Here is the full support url:</p> <textarea id="content"></textarea> </p> </body> </html>
I had not thought of that. I can just put the return value in a hidden element on my web page and then set the value to that in code. Thanks!