Hi All,
I've been struggling with this code, everything has been working until I tried to throw in the ability to identify. When I run the code, I get the error that identifyParams is undefined. Specifically, "Uncaught TypeError: Cannot set property 'geometry' of undefined"
Can anyone see what I've got wrong? Thanks in advance.
var map;
require([
"esri/InfoTemplate",
"esri/map",
"esri/dijit/BasemapToggle",
"esri/dijit/Popup",
"esri/dijit/Scalebar",
"esri/dijit/Search",
"esri/Color",
"esri/InfoTemplate",
"esri/layers/ArcGISDynamicMapServiceLayer",
"esri/layers/FeatureLayer",
"esri/layers/ImageParameters",
"esri/symbols/SimpleFillSymbol",
"esri/symbols/SimpleLineSymbol",
"esri/tasks/IdentifyTask",
"esri/tasks/IdentifyParameters",
"dojo/_base/array",
"dojo/dom",
"dojo/on",
"dojo/query",
"dojo/ready",
"dijit/registry",
"dojo/dom-construct",
"dojo/parser",
"dojo/domReady!"
],
function(
InfoTemplate,
Map,
BasemapToggle,
Popup,
Scalebar,
Search,
Color,
InfoTemplate,
ArcGISDynamicMapServiceLayer,
FeatureLayer,
ImageParameters,
SimpleFillSymbol,
SimpleLineSymbol,
IdentifyTask,
IdentifyParameters,
arrayUtils,
dom,
on,
query,
ready,
registry,
domConstruct,
parser
) {
var layer, visibleLayerIds = [], identifyTask, identifyParams;
var popup = new Popup({
fillSymbol: new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
new Color([255, 0, 0]), 2), new Color([255, 255, 0, 0.25]))
}, domConstruct.create("div"));
map = new Map("map", {
basemap: "topo",
center: [-123, 49.2],
zoom:10,
infoWindow: popup
});
//Use the ImageParameters to set the visibleLayerIds layers in the map service during ArcGISDynamicMapServiceLayer construction.
var imageParameters = new ImageParameters();
//VFPA Boundary line layers (confirmed and non-confirmed) visible on startup
imageParameters.layerIds = [0,2];
imageParameters.layerOption = ImageParameters.LAYER_OPTION_SHOW;
//can also be: LAYER_OPTION_EXCLUDE, LAYER_OPTION_HIDE, LAYER_OPTION_INCLUDE
var layerCadastral = new ArcGISDynamicMapServiceLayer("http://cop-gis4:6080/arcgis/rest/services/MOR/MOR_Cadastral_WGS1984/MapServer",
{
"imageParameters": imageParameters
});
map.addLayer(layerCadastral);
//every time a layer is clicked, update the layer visibility
on(dom.byId("chkVFPABoundary"), "change", updateLayerVisibility);
on(dom.byId("chkMunicipalBoundaries"), "change", updateLayerVisibility);
on(dom.byId("chkLots"), "change", updateLayerVisibility);
function updateLayerVisibility () {
var inputs = query(".list_item");
var inputCount = inputs.length;
//Dummy layer - is there a workaround? Setting this to an actual layer means that layer can't be turned off
visibleLayerIds = [1001];
for (var i = 0; i < inputCount; i++) {
if (inputs.checked) {
visibleLayerIds.push(inputs.value);
}
}
if (visibleLayerIds.length === 0) {
visibleLayerIds.push(-1);
}
layerCadastral.setVisibleLayers(visibleLayerIds);
}
//Basemap toggle
var toggle = new BasemapToggle({
map: map,
basemap: "satellite"
}, "BasemapToggle");
toggle.startup();
var scalebar = new Scalebar({
map: map,
// "dual" displays both miles and kilmometers
// "english" is the default, which displays miles
// use "metric" for kilometers
scalebarUnit: "dual"
});
var s = new Search({
enableButtonMode: false, //this enables the search widget to display as a single button
enableLabel: false,
enableInfoWindow: true,
showInfoWindowOnSelect: true,
map: map
}, "search");
var sources = s.get("sources");
//Push the sources used to search, by default the ArcGIS Online World geocoder is included.
sources.push({
featureLayer: new FeatureLayer("http://cop-gis4:6080/arcgis/rest/services/MOR/MOR_Cadastral_WGS1984/MapServer/7"),
searchFields: ["LOT_NO"],
exactMatch: false,
outFields: ["*"],
displayField: "LOT_NO",
name: "Lots - Lot Number",
placeholder: "Lot Number",
maxResults: 6,
maxSuggestions: 6,
//Create an InfoTemplate and include three fields
infoTemplate: new InfoTemplate("Lot Number", "Lot #: ${LOT_NO}</br>Owner: ${OWNER}</br>Title Link: <a href =${TITLE_LINK}>Click to open</a>"),
enableSuggestions: true,
minCharacters: 0
});
sources.push({
featureLayer: new FeatureLayer("http://cop-gis4:6080/arcgis/rest/services/MOR/MOR_Cadastral_WGS1984/MapServer/7"),
searchFields: ["OWNER"],
exactMatch: false,
outFields: ["*"],
displayField: "OWNER",
name: "Lots - Owner",
placeholder: "Owner",
maxResults: 6,
maxSuggestions: 6,
//Create an InfoTemplate and include three fields
infoTemplate: new InfoTemplate("Lot Number", "Lot #: ${LOT_NO}</br>Owner: ${OWNER}</br>Title Link: <a href =${TITLE_LINK}>Click to open</a>"),
enableSuggestions: true,
minCharacters: 0
});
//Set the sources above to the search widget
s.set("sources", sources);
s.startup();
map.on("load", mapReady);
function mapReady () {
map.on("click", executeIdentifyTask);
//create identify tasks and setup parameters
identifyTask = new IdentifyTask(layerCadastral);
identifyParams = new IdentifyParameters();
identifyParams.tolerance = 3;
identifyParams.returnGeometry = true;
identifyParams.layerIds = [7];
identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_VISIBLE;
identifyParams.width = map.width;
identifyParams.height = map.height;
}
function executeIdentifyTask (event) {
identifyParams.geometry = event.mapPoint;
identifyParams.mapExtent = map.extent;
var deferred = identifyTask
.execute(identifyParams)
.addCallback(function (response) {
// response is an array of identify result objects
// Let's return an array of features.
return arrayUtils.map(response, function (result) {
var feature = result.feature;
var layerName = result.layerName;
feature.attributes.layerName = layerName;
if (layerName === 'Lots') {
var lotsTemplate = new InfoTemplate("",
"Lot #: ${LOT_NO} <br/> Owner: ${OWNER}");
feature.setInfoTemplate(lotsTemplate);
}
else if (layerName === 'Building Footprints') {
console.log(feature.attributes.PARCELID);
var buildingFootprintTemplate = new InfoTemplate("",
"Parcel ID: ${PARCELID}");
feature.setInfoTemplate(buildingFootprintTemplate);
}
return feature;
});
});
// InfoWindow expects an array of features from each deferred
// object that you pass. If the response from the task execution
// above is not an array of features, then you need to add a callback
// like the one above to post-process the response and return an
// array of features.
map.infoWindow.setFeatures([deferred]);
map.infoWindow.show(event.mapPoint);
}
});
Solved! Go to Solution.
Laura,
You were trying to set the IdentifyTasks constructor to a layer object when it needs a url string.
corrected line:
identifyTask = new IdentifyTask(layerCadastral.url);
If you use Google Chrome, you can press F12 when you start up your application and then it usually tells you what it's looking for. Most of the time I have seen it as a typo or not declaring a variable with a var and then attempting to use it.
Hi Chris, I am using Chrome, and NetBeans. Using F12 unfortunately is not giving me any more enlightenment. I was able to get this esri sample working with my data but when I try to plop that into my existing map, I'm getting this error. I am fairly new to javascript and I think I've put something in the wrong order or something, but I don't know what.
Is your site public or can you post your page here: JS Bin - Collaborative JavaScript Debugging ?
It's not public nor are my services, but I've posted it and switched back to the service in the ESRI demo: JS Bin - Collaborative JavaScript Debugging
Laura,
You were trying to set the IdentifyTasks constructor to a layer object when it needs a url string.
corrected line:
identifyTask = new IdentifyTask(layerCadastral.url);
Thanks Robert, that did it!
So in the ESRI sample, they had set the IdentifyTasks constructor the same way I had - I just switched out my layer for theirs. I'm guessing it is to do with the difference between how they added their layer to the map:
var parcelsURL = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/Parcels/MapServer"; map.addLayer(new ArcGISDynamicMapServiceLayer(parcelsURL, { opacity: 0.55 }));
and how I added mine:
var layerCadastral = new ArcGISDynamicMapServiceLayer("http://cop-gis4:6080/arcgis/rest/services/MOR/MOR_Cadastral_WGS1984/MapServer", { "imageParameters": imageParameters }); map.addLayer(layerCadastral);
When I came to the line in the sample
identifyTask = new IdentifyTask(parcelsURL);
I just switched out parcelsURL for my layerCadastral. But in the sample parcelsURL was a variable that held the url string, so I'm guessing that's why it didn't work for me. Thanks both Robert and Chris for your assistance.