5

How do I change the extent on the map in function of a query?

in legend I have no problem using the function showResults (featureSet) {... but, can not get any display in AMD. Here I put a piece of script that I want to switch from legend to AMD:

function showResults(featureSet) {
  //remove all graphics on the maps graphics layer
  map.graphics.clear();
  //QueryTask returns a featureSet.  Loop through features in the featureSet and add them to the map.

  //Performance enhancer - assign featureSet array to a single variable.
  var resultFeatures = featureSet.features;
  var extent;
  for (var i = 0, il = resultFeatures.length; i < il; i++) {
    //Get the current feature from the featureSet.
    //Feature is a graphic
    var graphic = resultFeatures[i];
    graphic.setSymbol(symbol);
    geometry = graphic.geometry;
    ext = geometry.getExtent();
    if (extent) {
      extent = extent.union(ext);
    } else {
      extent = new esri.geometry.Extent(ext);
    }

    //Set the infoTemplate.
    graphic.setInfoTemplate(infoTemplate);

    //Add graphic to the map graphics layer.
    //map.addLayer(stateLayer);
    map.graphics.add(graphic);
  }

  map.setExtent(extent, true);

}
  • Check the spatial reference of output features returned by query task and spatial reference of map loaded. If the spatial reference is different then map.setExtent will not work. – bilwanath Jul 20 '17 at 20:46
  • What's AMD? I suggest writing it out in full the first time you use it in your question body, – PolyGeo Jul 20 '17 at 22:34
3

Depending on the kind of featureSet, here's a sample that shows how to zoom to the extent of the DirectionsFeatureSet result of a call to solve a route.

https://developers.arcgis.com/en/javascript/jssamples/routetask_directions.html

you might also want to check out the utility method 'graphicsExtent'. if you pass this method a collection of graphics it will return their extent, which can be passed to map.setExtent()

https://developers.arcgis.com/javascript/jsapi/esri.graphicsutils-amd.html#graphicsextent https://developers.arcgis.com/en/javascript/jsapi/map-amd.html#setextent

  • esri.graphicsExtent(featureSet) (added 3.8) is the way to go. It nicely returns an Extent instance, including the spatialReference, xmax, xmin, ymax, ymin. – iled Dec 18 '15 at 2:44
0

I am currently working with something similar, but I first set graphics for each feature in the featureSet and then add the graphics layer to the map (with map.graphics.add()), then iterate through the completed graphics array to get the union extent of its elements, using layer.graphics[i].geometry,getExtent(), then extent.union(ext), and finally map.setExtent(extent). This works for JS API 3.8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.