I currently have a featurelayer being queried, and the results placed in a graphicslayer ... I am hoping that the results can be placed in a new featurelayer instead?
Here is current code:
$("#radio-4").click(function() { //shows only January crimes
var sr = new SpatialReference(102100);
janResults = new GraphicsLayer();
janResults.title = "January";
map.add(janResults);
var janQuery = featurelayer.createQuery();
janQuery.where = "Month = 'January'";
janQuery.outFields = ["Offense" , "Location"];
janQuery.returnGeometry = true;
janQuery.outSpatialReference = sr;
featurelayer.queryFeatures(janQuery).then(displayGraphics);
function displayGraphics(results) {
janResults.removeAll();
for (feature of results.features) {
var janGraphic = new Graphic ({
geometry: feature.geometry,
symbol: {
type: "simple-marker",
color: "blue",
size: "20px",
outline: {
color:"black",
width: 2,
},
},
});
janResults.add(janGraphic);
}
}
}); //end of January query
Solved! Go to Solution.
John,
Sure you just need to create the FL from the graphics array.
var sRenderer = {
type: "simple", // autocasts as new SimpleRenderer()
symbol: {
type: "simple-marker", // autocasts as new SimpleMarkerSymbol()
size: "20px",
color: "blue,
outline: {
width: 2,
color: "black"
}
}
};
var graArr = [];
var janQuery = featurelayer.createQuery();
janQuery.where = "Month = 'January'";
janQuery.outFields = ["Offense" , "Location"];
janQuery.returnGeometry = true;
janQuery.outSpatialReference = sr;
featurelayer.queryFeatures(janQuery).then(displayGraphics);
function displayGraphics(results) {
for (feature of results.features) {
var janGraphic = new Graphic ({
geometry: feature.geometry,
attributes: {
Offense: feature.attributes.Offense,
Location: feature.attributes.Location
}
});
graArr.push(janGraphic);
}
createLayer(graArr);
}
function createLayer(graphics) {
var fields = [{
name: "ObjectID",
alias: "ObjectID",
type: "oid"
}, {
name: "Offense",
alias: "Offense",
type: "string"
}, {
name: "Location",
alias: "Location",
type: "string"
}];
layer = new FeatureLayer({
title: "January",
source: graphics,
// create an instance of esri/layers/support/Field for each field object
fields: fields, // This is required when creating a layer from Graphics
objectIdField: "ObjectID", // This must be defined when creating a layer from Graphics
renderer: sRenderer, // set the visualization on the layer
});
map.add(layer);
}
John,
Sure you just need to create the FL from the graphics array.
var sRenderer = {
type: "simple", // autocasts as new SimpleRenderer()
symbol: {
type: "simple-marker", // autocasts as new SimpleMarkerSymbol()
size: "20px",
color: "blue,
outline: {
width: 2,
color: "black"
}
}
};
var graArr = [];
var janQuery = featurelayer.createQuery();
janQuery.where = "Month = 'January'";
janQuery.outFields = ["Offense" , "Location"];
janQuery.returnGeometry = true;
janQuery.outSpatialReference = sr;
featurelayer.queryFeatures(janQuery).then(displayGraphics);
function displayGraphics(results) {
for (feature of results.features) {
var janGraphic = new Graphic ({
geometry: feature.geometry,
attributes: {
Offense: feature.attributes.Offense,
Location: feature.attributes.Location
}
});
graArr.push(janGraphic);
}
createLayer(graArr);
}
function createLayer(graphics) {
var fields = [{
name: "ObjectID",
alias: "ObjectID",
type: "oid"
}, {
name: "Offense",
alias: "Offense",
type: "string"
}, {
name: "Location",
alias: "Location",
type: "string"
}];
layer = new FeatureLayer({
title: "January",
source: graphics,
// create an instance of esri/layers/support/Field for each field object
fields: fields, // This is required when creating a layer from Graphics
objectIdField: "ObjectID", // This must be defined when creating a layer from Graphics
renderer: sRenderer, // set the visualization on the layer
});
map.add(layer);
}
Thank you Robert ... I will give this a shot!
dojo.js:251 [esri.layers.FeatureLayer] #load() Failed to load layer (title: 'January', id: '166785b54b9-layer-0')
I get the above error message in console ...
John,
make this change:
layer = new FeatureLayer({
geometryType: "point",
title: "January",
source: graphics,
// create an instance of esri/layers/support/Field for each field object
fields: fields, // This is required when creating a layer from Graphics
objectIdField: "ObjectID", // This must be defined when creating a layer from Graphics
renderer: sRenderer, // set the visualization on the layer
});
map.add(layer);
Interestingly, no errors are thrown, the symbol for January shows up in the legend, the layer shows up as turned on in the layer list, but nothing is shown on the map!
And when you were adding this as a graphics layer there where results right?
Yes, many
John,
I was just guessing at the geometry type when I choose point. Is the data actually point data or some other geometry type?
It is point data, yes