I am performing a query on a featurelayer. The query is returning results (I can see results in the console.log), but when i try to view.toGo(returnedFeature) nothing happens. I have tried fixed Point, and a lot of other things, but nothing seems to work. I have set up a button that when you click it, it does the query.
I am using the 4.2 API
require([
"esri/Map",
"esri/views/SceneView",
"esri/views/MapView",
"esri/layers/FeatureLayer",
"esri/tasks/QueryTask",
"esri/tasks/support/Query",
"esri/geometry/Point",
"dojo/on",
"dojo/domReady!"
],
function (
Map,
SceneView,
MapView,
FeatureLayer,
QueryTask,
Query,
Point,
on
) {
var template = {
title: "{Description}",
content: "<p>Asset ID:<b>{AssetID}</b></p>" +
"<p>Asset Status:<b>{AssetStatus}</b></p>"
};
var url = "http://tga-app-sandbox.tauranga.govt.nz:6080/arcgis/rest/services/TreeApp/TreeCapture_AllMaintainenc...";
var featureLayer = new FeatureLayer({
url: url,
outFields: ["*"],
popupTemplate: template
});
var map = new Map({
basemap: "hybrid",
spacialReference: featureLayer.spacialReference
});
var view = new MapView({
container: "viewDiv",
map: map,
zoom: 16,
center: [176.2, -37.7],
spacialReference: featureLayer.spacialReference
});
on(dojo.query("#default"), "click", function () {
var params = new Query({
returnGeometry: true,
outFields: ["*"]
});
params.where = 'AssetID = 141971';
var qTask = new QueryTask({
url: url
});
qTask.execute(params)
.then(function (response) {
if (!response || !response.features) {
console.log("Asset not found.");
return;
}
console.log(response.features);
view.goTo(response.features[0], { zoom: 18 });
//view.goTo({
// target: new Point({
// x: response.features[0].geometry.x,
// y: response.features[0].geometry.y,
// spatialReference: response.features[0].geometry.spatialReference
// }), zoom: 18
//});
})
.otherwise(function (err) {
console.log(err);
});
});
map.add(featureLayer);
view.then(function () {
var params = new Query({
returnGeometry: true,
outFields: ["*"]
});
params.where = 'AssetID = 115576';
var qTask = new QueryTask({
url: url
});
qTask.execute(params)
.then(function (response) {
if (!response || !response.features) {
console.log("Asset not found.");
return;
}
console.log(response.features);
view.goTo(response.features, {zoom: 5});
})
.otherwise(function (err) {
console.log(err);
});
}, function(error){
console.log( error);
});
});
What am i doing wrong? Neither of my view.goTo functions work?!
Solved! Go to Solution.
Tough to to tell, that service isn't public. You do have spatialReference misspelled in a few places though.
Also, if you are using a non-webmercator spatialReference, you can't use the basemap strings, you have to create an empty map.
Brain,
It looks like a scope issue to me. When you call qTask.execute the deferred response is in a different scope. Do you see an error in your browsers console that says goTo is not a function of undefined or null? If that is the case then this is where you would use dojo/_base/lang reguire and lang.htich to maintain your codes scope or you need to make your view a global var instead of a local one.
There are no errors in the console. Here is the output after i refresh the page, then click the button:
I also just tried setting view to a global variable, and it still doesn't work / no errors in console 😕
Tough to to tell, that service isn't public. You do have spatialReference misspelled in a few places though.
Also, if you are using a non-webmercator spatialReference, you can't use the basemap strings, you have to create an empty map.
I got it working by creating my own Basemap and removing all spatialReference's.
Cheers!