Select to view content in your preferred language

Zoom to Selected Polygon

4082
10
09-19-2013 01:27 PM
PatrickThorsell
Emerging Contributor
My code will select and highlight the parcel that is clicked on, but it zooms in to the same spot on the map no matter which parcel I click on. It returns the correct parcel #, and there is only 1 record selected, but it keeps zooming to exact same spot. Any ideas?

for (var i = 0, il = results.features.length; i < il; i++) {

                var graphic = results.features;
                graphic.setSymbol(symbol);
                map.graphics.add(graphic);
                map.centerAndZoom(results.features.geometry, 0.1);
                
                alert(results.features.length); //this returns 1
 }
0 Kudos
10 Replies
JasonZou
Frequent Contributor
Change:
for (var i = 0, il = results.features.length; i < il; i++) {

                var graphic = results.features;
                graphic.setSymbol(symbol);
                map.graphics.add(graphic);
                map.centerAndZoom(results.features.geometry, 0.1);
                
                alert(results.features.length); //this returns 1
 }


To:
var graphic;
for (var i = 0, il = results.features.length; i < il; i++) {
      graphic = results.features;
      graphic.setSymbol(symbol);
      map.graphics.add(graphic);
 }
map.setExtent(esri.graphicsExtent(results.features));
0 Kudos
PatrickThorsell
Emerging Contributor
I made the changes you suggested zj_zou, but the code bombs on this line:
map.setExtent(esri.graphicsExtent(results.features));
0 Kudos
JasonZou
Frequent Contributor
Which version are you using for Javascript API?
0 Kudos
PatrickThorsell
Emerging Contributor
0 Kudos
JasonZou
Frequent Contributor
Assume you are using AMD style.

Make sure to include "esri/graphicsUtils" to the dependency list, and take graphicsUtils as the alias.
var resultExtent = graphicsUtils.graphicsExtent(results.features);
map.setExtent(resultExtent);
0 Kudos
PatrickThorsell
Emerging Contributor
Code still gets hung up, on this line:
var resultExtent = graphicsUtils.graphicsExtent(results.features);
0 Kudos
JasonZou
Frequent Contributor
Questions:

  1. What error message did you get?

  2. What is the spatial type of the parcel data, point or polygon?

  3. If points, does the map use any tiled/cached basemap?



The code I provided should work for polygons. It won't work for points. For points, here is the code.

For the map with only dynamic map services/layers loaded,
map.centerAndZoom(graphic, 0.5);


For the map with tiled basemap loaded,
map.centerAndZoom(graphic, 12);   // change 12 to any zoom level you like
0 Kudos
PatrickThorsell
Emerging Contributor
1. What error message did you get? No error, the map just 'spins' and I narrowed it down to that line of code.
2. What is the spatial type of the parcel data, point or polygon? polygon
3. If points, does the map use any tiled/cached basemap? Polygonss

The code I provided should work for polygons. It won't work for points. For points, here is the code.

No map showed up when i used this code:
map.centerAndZoom(graphic, 0.5);


No map showed up with this code:
map.centerAndZoom(graphic, 12);  


Map showed up, but it was just zoomed in to the middle of my data's extent with this code:
map.centerAndZoom(graphic.geometry, 0.1);
0 Kudos
PatrickThorsell
Emerging Contributor
I figured it out, it was a projection issue 😞
Thanks for your help!
0 Kudos