Sunday, 23 October 2016

json - Adding Layer using esriRequest Response in ArcGIS API for JavaScript?


For various reasons I need to add layers to a map object using a response returned by an esriRequest to an ArcGIS Server. One reason being the data will change constantly and hard coding FeatureLayer URLs would prove inefficient.


I have successfully been able to return a JSON object containing attribution that appears to be the same data one would see on the layers REST node.


 maps = [];


require([
"esri/map", "esri/layers/FeatureLayer",
"esri/dijit/Print", "esri/tasks/PrintTemplate", "esri/request",
"esri/config","esri/dijit/BasemapToggle",

"dojo/_base/array", "dojo/dom", "dojo/parser", "dojo/domReady!"

], function (Map, FeatureLayer,Print, PrintTemplate, esriRequest, esriConfig,BasemapToggle,arrayUtils, dom, parser){


parser.parse();

console.log("Map Start");

esriConfig.defaults.io.proxyUrl = "http://........./proxy.ashx";

maps.addRespLyrs = function addRespLyrs (layer) {
if (layer.type == "Feature layer") {
console.log(layer.name);
maps.map.addLayer(layer);

}
};

maps.handleMapInfo = function handleMapInfo (resp) {
console.log("handleMapInfo called");
maps.layers = resp.layers;
console.log(maps.layers);
for (i in maps.layers) {
var layer = maps.layers[i];
maps.addRespLyrs(layer);

}
};

maps.layerRequest = function layerRequest () {
var mapInfo = esriRequest({
"url": "https://........./Application Name/MapServer/layers",
"content" : {"f":"json"},
"handleAs" : "json",
"callbackParamName" : "callback"
});

mapInfo.then(maps.handleMapInfo);
};

maps.map = new Map("cpCentCent", {
basemap:"topo",
center: [-98.5795, 39.8282],
zoom: 5
});



console.log("Map End");

When attempting to add the data using the addLayer() method I recieve an error thrown from deep with in esri's API.



init.js:199 TypeError: a.id.match is not a function(…) "TypeError: a.id.match is not a function



Any thoughts as to what could be going wrong here or how to create a layer from my JSON?



Answer



I think you need to be using esri/layers/FeatureLayer somewhere in here, because the maps.map.addLayer method is expecting a layer class as input. It looks like you're just passing in a json object instead of a layer class object.


Trap the URL from your esriRequest and build a new FeatureLayer from it, something like:



featLayer = new FeatureLayer(urlFromRequest);
maps.map.addLayer(featLayer);

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...