zooming on a object id for the first time is working but afterwards it showing the error " [{},"TypeError: Cannot read property 'geometry' of undefined"

714
6
Jump to solution
03-14-2019 05:14 AM
ShakyasinghMohapatra
New Contributor II

 $("#btnSearch").click(function () {
zoomRow($("#selDivRoad1").val());
});

function zoomRow(id) {

alert("id = "+id);
var roadsLayer1 = new esri.layers.FeatureLayer("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", {
id : "roadLayerId1",
mode: FeatureLayer.MODE_ONDEMAND,
outFields: ["*"],

infoTemplate: new PopupTemplate({
title: "ROAD ID : {****}",
description: "<br />ROAD NAME: {***}"
+ "<br />ROAD CATEGORY: {***}"
+ "<br />OBJECTID: {OBJECTID}"
})
});
//roadsLayer1.clearSelection();
var fieldsSelectionSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,new Color([255,0,0]),3);
roadsLayer1.setSelectionSymbol(fieldsSelectionSymbol);
map.addLayer(roadsLayer1);
var query = new Query();
query.where="OBJECTID="+id;
query.geometry = map.extent;
query.spatialRelationship = Query.SPATIAL_REL_ENVELOPEINTERSECTS;
query.returnGeometry = true;

query.outFields = ["ObjectID","*****","***","****"];
roadsLayer1.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW, function(features) {


var stateExtent = features[0].geometry.getExtent().expand(5.0);
map.setExtent(stateExtent);

});

}

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

Here is my suggested changes. Call the initLayer() function somewhere in your apps startup. Notice line 23 below is commented out. This is what I was telling you to do earlier.

function initLayer() {
  var roadsLayer1 = new esri.layers.FeatureLayer("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", {
    id: "roadLayerId1",
    mode: FeatureLayer.MODE_ONDEMAND,
    outFields: ["*"],
    infoTemplate: new PopupTemplate({
      title: "ROAD ID : {****}",
      description: "<br />ROAD NAME: {***}" +
        "<br />ROAD CATEGORY: {***}" +
        "<br />OBJECTID: {OBJECTID}"
    })
  });
  var fieldsSelectionSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0]), 3);
  roadsLayer1.setSelectionSymbol(fieldsSelectionSymbol);
  map.addLayer(roadsLayer1);
}


function zoomRow(id) {
  alert("id = " + id);
  var query = new Query();
  query.where = "OBJECTID=" + id;
  //query.geometry = map.extent;   //Don't do this line
  query.spatialRelationship = Query.SPATIAL_REL_ENVELOPEINTERSECTS;
  query.returnGeometry = true;

  query.outFields = ["ObjectID", "*****", "***", "****"];
  roadsLayer1.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW, function(features) {
    var stateExtent = features[0].geometry.getExtent().expand(5.0);
    map.setExtent(stateExtent);
  });
}

View solution in original post

0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Shakyasingh,

   So your query uses the maps extent:

query.geometry = map.extent;

So the next time you query after the maps extent has already been zoomed in from the first result the results of the second query is very likely null and thus does not have any geometry.

Another big issue I see in the code is that you add a new featureLayer each time the search button is clicked...

0 Kudos
ShakyasinghMohapatra
New Contributor II

I will change the code so that a new featureLayer must not be added each time the search button is clicked

but how to solve the null issue , please guide 

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

I answered that in the first part of my reply. Don't use the maps extent in the query.

0 Kudos
ShakyasinghMohapatra
New Contributor II

Can You please suggest some code , I am a new comer to GIS web Development so I think I am missing something structurally.

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Here is my suggested changes. Call the initLayer() function somewhere in your apps startup. Notice line 23 below is commented out. This is what I was telling you to do earlier.

function initLayer() {
  var roadsLayer1 = new esri.layers.FeatureLayer("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", {
    id: "roadLayerId1",
    mode: FeatureLayer.MODE_ONDEMAND,
    outFields: ["*"],
    infoTemplate: new PopupTemplate({
      title: "ROAD ID : {****}",
      description: "<br />ROAD NAME: {***}" +
        "<br />ROAD CATEGORY: {***}" +
        "<br />OBJECTID: {OBJECTID}"
    })
  });
  var fieldsSelectionSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0]), 3);
  roadsLayer1.setSelectionSymbol(fieldsSelectionSymbol);
  map.addLayer(roadsLayer1);
}


function zoomRow(id) {
  alert("id = " + id);
  var query = new Query();
  query.where = "OBJECTID=" + id;
  //query.geometry = map.extent;   //Don't do this line
  query.spatialRelationship = Query.SPATIAL_REL_ENVELOPEINTERSECTS;
  query.returnGeometry = true;

  query.outFields = ["ObjectID", "*****", "***", "****"];
  roadsLayer1.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW, function(features) {
    var stateExtent = features[0].geometry.getExtent().expand(5.0);
    map.setExtent(stateExtent);
  });
}
0 Kudos
ShakyasinghMohapatra
New Contributor II

thanks a lot robert

After commenting the below line 

 //query.geometry = map.extent;

it worked 

Thank you so much...

0 Kudos