Extent.contains returns false for a polyline in the extent

1596
2
Jump to solution
12-17-2013 09:30 AM
KenBuja
MVP Esteemed Contributor
I am using Extent.contains in a function to see if a graphic is inside the current map extents before I do some effects with it. The effect crashes if the graphic is not within the map's extent. This function receives points and lines, both of which are in the same dynamic map service with the same spatial reference (10200).

I've set up an example in JSBin to show this. It queries the line layer, checks whether it's within the map extent, and adds the graphic to the map. It does the same to the point layer. The console reports the map extent and the feature extent. Evaluating the point returns true, but the line returns false. Why isn't the line feature seen as being inside the map extent?
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi Ken,

The input parameter for the contains method can be a point or extent, so try creating a new Extent object for the line's extent.  Be sure to load the Extent and SpatialReference modules.  Ex:

function flashFeature(graphic) {      var text;       if (graphic.geometry.type == "point") {        var extent = graphic.geometry;        text = "Point: " + graphic.geometry.x + ", " + graphic.geometry.y;      }      else if (graphic.geometry.type == "polyline") {             var lineExtent = graphic.geometry.getExtent();                                          var extent = new Extent(lineExtent.xmin, lineExtent.ymin, lineExtent.xmax, lineExtent.ymax, new SpatialReference({ wkid:102100}));                                          text = "Line: " + extent.xmin + ", " + extent.ymin + ", " + extent.xmax + ", " + extent.ymax;       }      if (!map.extent.contains(extent)) {        console.log("Outside current extent - " + text);      }      else {        console.log("Inside current extent - " + text);      }       map.graphics.add(graphic);  }

View solution in original post

0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Ken,

The input parameter for the contains method can be a point or extent, so try creating a new Extent object for the line's extent.  Be sure to load the Extent and SpatialReference modules.  Ex:

function flashFeature(graphic) {      var text;       if (graphic.geometry.type == "point") {        var extent = graphic.geometry;        text = "Point: " + graphic.geometry.x + ", " + graphic.geometry.y;      }      else if (graphic.geometry.type == "polyline") {             var lineExtent = graphic.geometry.getExtent();                                          var extent = new Extent(lineExtent.xmin, lineExtent.ymin, lineExtent.xmax, lineExtent.ymax, new SpatialReference({ wkid:102100}));                                          text = "Line: " + extent.xmin + ", " + extent.ymin + ", " + extent.xmax + ", " + extent.ymax;       }      if (!map.extent.contains(extent)) {        console.log("Outside current extent - " + text);      }      else {        console.log("Inside current extent - " + text);      }       map.graphics.add(graphic);  }
0 Kudos
KenBuja
MVP Esteemed Contributor
Thanks Jake,

Could the documentation be changed to reflect this better? Right now, the code snippet doesn't show the need to get the extent of the graphic. Currently, it is just

var extent = map.extent;
if(extent.contains(graphic.geometry)) {
  graphic.setSymbol(highlightSymbol);
}


In my case, since I know the input graphics will always have the same spatial reference, I can use

function flashFeature(graphic) {
     var text, extent;

     if (graphic.geometry.type == "point") {
         extent = graphic.geometry;
         text = "Point: " + graphic.geometry.x + ", " + graphic.geometry.y;
     }
     else if (graphic.geometry.type == "polyline") {
         extent = graphic.geometry.getExtent();                             
         text = "Line: " + extent.xmin + ", " + extent.ymin + ", " + extent.xmax + ", " + extent.ymax;

     }
     if (!map.extent.contains(extent)) {
         console.log("Outside current extent - " + text);
     }
     else {
         console.log("Inside current extent - " + text);
     }

     map.graphics.add(graphic);

}
0 Kudos