Zoom to single point

3532
2
Jump to solution
10-10-2013 12:13 PM
GeoffSchwitzgebel
New Contributor
Hello,

I currently have a query that will return 1 or multiple points.  The problem I am having is getting the query to zoom to a single point.  Here is my code, there is a valid feature set being returned:
                function showResults(featureSet) {                     //remove all graphics on the maps graphics layer                     map.graphics.clear();                     //Performance enhancer - assign featureSet array to a single variable.                     var resultFeatures = featureSet.features;                     //Loop through each feature returned                     for (var i = 0, il = resultFeatures.length; i < il; i++) {                         //Get the current feature from the featureSet.                         //Feature is a graphic                         var graphic = resultFeatures;                         graphic.setSymbol(symbol);                         //Set the infoTemplate.                         graphic.setInfoTemplate(infoTemplate);                         //Add graphic to the map graphics layer.                         nameGraphic = map.graphics.add(graphic);                         //set map extent to selected features                                                 //This works if more than one point//                         //var myFeatureExtent = esri.graphicsExtent(resultFeatures);                         //map.setExtent(myFeatureExtent);                          onQueryComplete(resultFeatures); //Want to use this function to determine zoom                     }                              }

I also have a function that can be used to zoom to the appropriate area:
                function onQueryComplete(returnedPointFeatureSet) {                     var featureSet = returnedPointFeatureSet || {};                     var features = featureSet.features || [];                      var extent = esri.graphicsExtent(features);                     if (!extent && features.length == 1) {                         var point = features[0];                         map.centerAndZoom(point, 3);                     }                     else {                         map.setExtent(extent);                     }                 }

I'm having an issue with getting the onQueryComplete function to work properly.  I don't see why it will not work.  Any assistance is appreciated.

Thanks,
Geoff
0 Kudos
1 Solution

Accepted Solutions
SteveCole
Frequent Contributor
With a single point returned, try expanding the extent that you extract from the point. Something like this:
  var theExtent = features[0].geometry.getExtent().expand(1.5);   map.setExtent(theExtent);


In my own apps, this is how I expand the extent of a point feature:
var thePoint = features[0].geometry; var theExtent = pointToExtent(map,thePoint,15); map.setExtent(theExtent);  //============================================================================= // Utility routine to convert a point's geographic location into a rectangle. // Used to provide a zoom extent for point features //============================================================================= function pointToExtent(map, point, toleranceInPixel) {  //Function to convert a point coordinate into a rectangle area  var pixelWidth = map.extent.getWidth() / map.width;  var toleraceInMapCoords = toleranceInPixel * pixelWidth;   return new esri.geometry.Extent( point.x - toleraceInMapCoords,      point.y - toleraceInMapCoords,      point.x + toleraceInMapCoords,      point.y + toleraceInMapCoords,      map.spatialReference  ); }


Good luck!
Steve

View solution in original post

0 Kudos
2 Replies
SteveCole
Frequent Contributor
With a single point returned, try expanding the extent that you extract from the point. Something like this:
  var theExtent = features[0].geometry.getExtent().expand(1.5);   map.setExtent(theExtent);


In my own apps, this is how I expand the extent of a point feature:
var thePoint = features[0].geometry; var theExtent = pointToExtent(map,thePoint,15); map.setExtent(theExtent);  //============================================================================= // Utility routine to convert a point's geographic location into a rectangle. // Used to provide a zoom extent for point features //============================================================================= function pointToExtent(map, point, toleranceInPixel) {  //Function to convert a point coordinate into a rectangle area  var pixelWidth = map.extent.getWidth() / map.width;  var toleraceInMapCoords = toleranceInPixel * pixelWidth;   return new esri.geometry.Extent( point.x - toleraceInMapCoords,      point.y - toleraceInMapCoords,      point.x + toleraceInMapCoords,      point.y + toleraceInMapCoords,      map.spatialReference  ); }


Good luck!
Steve
0 Kudos
GeoffSchwitzgebel
New Contributor
Thanks Steve.  Instead of calling a separate function, I added the if/else into the showResults script.  Your code:

var thePoint = features[0].geometry;
var theExtent = pointToExtent(map,thePoint,15);
map.setExtent(theExtent);


helped me realize that I wasn't getting the geometry, just the feature set.  Once I added the .geometry in, I was good to go.  Here is my final script:
                function showResults(featureSet) {
                    //remove all graphics on the maps graphics layer
                    map.graphics.clear();
                    //Performance enhancer - assign featureSet array to a single variable.
                    var resultFeatures = featureSet.features;
                    //Loop through each feature returned
                    for (var i = 0, il = resultFeatures.length; i < il; i++) {
                        //Get the current feature from the featureSet.
                        //Feature is a graphic
                        var graphic = resultFeatures;
                        graphic.setSymbol(symbol);
                        //Set the infoTemplate.
                        graphic.setInfoTemplate(infoTemplate);
                        //Add graphic to the map graphics layer.
                        nameGraphic = map.graphics.add(graphic);
                        //set map extent to selected features

                        if (resultFeatures.length == 1) {
                            var thePoint = resultFeatures[0].geometry;
                            map.centerAndZoom(thePoint, 3);
                        }
                        else {
                            var myFeatureExtent = esri.graphicsExtent(resultFeatures);
                            map.setExtent(myFeatureExtent);
                        }


                    }             
                }


Thanks for your help.

Geoff
0 Kudos