Proper use of  "setExtent()"

818
3
Jump to solution
01-26-2012 01:30 PM
MatthewZetterholm
New Contributor
Regarding the code at the bottom...

I am trying to change the map extent on the fly.

My map opens fine to an original extent.  I pan away a little bit.  I click a button to move it back to the original extent, but it never moves.

I know the function executes because I get the alert.

Also, when I run it with the last statement (which is commented out here) the map zooms, so I know the setExtent() method will at least work.

The wkid (102100) is used by the arcgis.com map server.

Am I misunderstanding the correct use of setExtent()?

Thanks.


 var contUnitedStatesExtent  = new esri.geometry.Extent({"xmin":-14327455,"ymin":3000000,"xmax":-8199999,"ymax":6000000,"spatialReference":{"wkid":102100}});  map1 = new esri.Map("mapDiv", {extent:contUnitedStatesExtent, wrapAround180:true});  basemap1 = new esri.layers.ArcGISTiledMapServiceLayer  ( "http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" );  map1.addLayer( basemap1 );   <!-- Change Extent --> function changeExtent () {      alert("Inside changeExtent()");         map1.setExtent(contUnitedStatesExtent, true);       //map1.setExtent(map1.extent.expand(0.5)); }
0 Kudos
1 Solution

Accepted Solutions
KellyHutchins
Esri Frequent Contributor
Matt,

I suspect that if you install Firebug or look in the Chrome Developer tools you'll see that your code that zooms back to the initial extent is throwing an error about the contUnitedStatesExtent not being defined. In your code you define the contUnitedStatesExtent in a function then try to access that variable from another function where it's not available. One option would be to do something like this instead:

      var map;       var initExtent;       function init() {         initExtent = new esri.geometry.Extent({"xmin":-122.46,"ymin":37.73,"xmax":-122.36,"ymax":37.77,"spatialReference":{"wkid":4326}});         map = new esri.Map("map",{           extent:esri.geometry.geographicToWebMercator(initExtent)         });         //Add the topographic layer to the map. View the ArcGIS Online site for services http://arcgisonline/home/search.html?t=content&f=typekeywords:service             var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");         map.addLayer(basemap);       }       function setExtent(){         map.setExtent(esri.geometry.geographicToWebMercator(initExtent));       }        dojo.addOnLoad(init);

View solution in original post

0 Kudos
3 Replies
EdSaunders
Occasional Contributor
Matt, I'm relatively new to JavaScript and in particular the AGS JSAPI but...I believe you can only use the expand method on the geometry extent class, not the map extent.  If you want to use expand here, you'd have to declare your extent as new geometry first and then expand it.
0 Kudos
KellyHutchins
Esri Frequent Contributor
Matt,

I suspect that if you install Firebug or look in the Chrome Developer tools you'll see that your code that zooms back to the initial extent is throwing an error about the contUnitedStatesExtent not being defined. In your code you define the contUnitedStatesExtent in a function then try to access that variable from another function where it's not available. One option would be to do something like this instead:

      var map;       var initExtent;       function init() {         initExtent = new esri.geometry.Extent({"xmin":-122.46,"ymin":37.73,"xmax":-122.36,"ymax":37.77,"spatialReference":{"wkid":4326}});         map = new esri.Map("map",{           extent:esri.geometry.geographicToWebMercator(initExtent)         });         //Add the topographic layer to the map. View the ArcGIS Online site for services http://arcgisonline/home/search.html?t=content&f=typekeywords:service             var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");         map.addLayer(basemap);       }       function setExtent(){         map.setExtent(esri.geometry.geographicToWebMercator(initExtent));       }        dojo.addOnLoad(init);
0 Kudos
MatthewZetterholm
New Contributor
That was it.

"contUnitedStatesExtent" was, in fact, out of scope. 

Thanks for the help, Kelly.



(And thanks for the effort, bundarra.)
0 Kudos