setExtent() doesn't take effect

2821
7
05-04-2010 01:43 PM
LarsHuttar
New Contributor III
Hi... I'm really puzzled and blocked by this one.
Here I have code in one of the map's onLoad event handlers, where I'm attempting to set the extent of the map based on params in the URL:

alert('setting extent according to url');
var newExtent = new esri.geometry.Extent(params.xmin, params.ymin, params.xmax, params.ymax,
map.spatialReference);
alert('new extent: ' + newExtent.xmin + ', ' + newExtent.xmax + ', ' + newExtent.ymin + ', ' + newExtent.ymax);
map.setExtent(newExtent);
var pe = map.extent;
alert('post extent: ' + pe.xmin + ', ' + pe.xmax + ', ' + pe.ymin + ', ' + pe.ymax);


The alerts show clearly that the "post extent" (the map extent after I call setExtent()) is not the same as the "new extent" (the extent passed to setExtent()). In fact the map extent seems to be totally unaffected by my call to setExtent().
There are no errors thrown either, that I can tell. I'm running in Firefox with Firebug, so I should be notified if there are errors.

The same thing also occurs with IE 8.

Anyone have ideas?

ArcGIS Server 9.3.1, JS API 1.5.
0 Kudos
7 Replies
LarsHuttar
New Contributor III
Further notes...

I'm calling setExtent() in a map onload event handler. So the map should already be loaded by the time I call setExtent().

I also tried setting a timer (250 ms) from the map onload handler, and then calling setExtent() from the timer event handler. That didn't help... setExtent still has no effect.

ESRI tech support suggested I try the new 2.0 version of the JS API, instead of 1.5. So I did. No change in the behavior.
0 Kudos
DerekSwingley
Occasional Contributor
Can you post a more complete code snippet? I can confirm that setExtent() works in 1.5. Not sure why support would tell you to go to 2.0 for this (there are plenty of other good reasons, I just don't think this is one of them :)). It's a basic function that, AFAIK, has not been broken in any version of the API.
0 Kudos
LarsHuttar
New Contributor III
Swingley, thanks for your effort to help.

It turned out that the problem was that in my extent constructor,
new esri.geometry.Extent(params.xmin, params.ymin, params.xmax, params.ymax,
   map.spatialReference);

the first four values I'm passing in are strings... (naturally enough, since they are just snipped out of the URL, as returned by esri.urlToObject(window.location.href).query).

I assumed javascript would convert them to numbers if required, or the constructor would raise an error or do the conversion itself. Unfortunately none of these is the case.

When I change the constructor to explicitly convert to numbers, e.g.
new esri.geometry.Extent(parseFloat(params.xmin), parseFloat(params.ymin), etc.);
then everything works fine.

My request for ESRI would be that the Extent constructor either accept strings as arguments and parse them into numbers, or throw an error when passed non-numbers. That would be more robust API design. Silently creating an object that doesn't work, tends to waste a lot of developer time.
0 Kudos
DerekSwingley
Occasional Contributor
Glad to help. That's one of the perils of JavaScript; no type checking. It's a bit more work from time to time but I still prefer it to strongly typed languages. Anyway, I don't think the API should be doing type checking...it's gotta leave a little work for us :).
0 Kudos
VanithaV
New Contributor
@huttarl
I am having the same issue as setExtent not working and i tried converting the string to float as you mentioned but no luck.What i am actually trying is when they click save ,it should refresh the map with same location.Please see the attached code.
I am also thinking of version of the script.

Thanks,
0 Kudos
ReneRubalcava
Frequent Contributor
I'm having a similar problem with map.setExtent().
It will work the first time that I try it, but will refuse to set the extent after that.

Here is how I load my features into my map.
function onResults(results) {
    var features = results.features;
    var len = features.length;
    annexDict = {};
    map.graphics.clear();
 var sel = dojo.byId("annexSelect");
    while (sel.hasChildNodes()) {
  sel.removeChild(sel.lastChild);
 }
    
    var j = 0;
    for (j = 0; j < features.length; j++) {
  var feature = features;
  var item = feature.attributes;
  var c = dojo.doc.createElement("option");
  c.innerHTML = "TEST: " + item.ANNEXATION;
  c.value = item.ANNEXATION;
  sel.appendChild(c);
  annexDict[c.value] = {
   annex: item.ANNEXATION,
   graphic: feature
  }
                          map.graphics.add(feature);
    }
}


My results get added a Multiselect list and a dictionary object.
Then when an item in the list is clicked, I handle it like this.
function onAnnexInListClicked(e) {
    console.log("option was clicked");
    if (e.target) {
  try {
   var targ = e.target;
   console.log(targ.value);
   var item = annexDict[targ.value];
   console.log("from dictionary: Annexation = " + item.annex);
   var g = item.graphic;
   var geom = g.geometry;
   var ext = geom.getExtent();
   
   // tried to explicitly create a new extent, but same result
   var xmin = ext.xmin;
   var ymin = ext.ymin;
   var xmax = ext.xmax;
   var ymax = ext.ymax;
   var sr = new esri.SpatialReference({wkid: 102100});

   var gExt = new esri.geometry.Extent(xmin, ymin, xmax, ymax, sr);
   map.setExtent(gExt);
   // map.setExtent(ext); // this had the same result
   console.log("map extent changed");
  } catch(err) {
   console.log("error in setting map extent to graphic extent");
   console.log(err);
  }
    }
}


The first time an item is clicked, the map will zoom to the proper location.
After that, all subsequent requests to change the map extent seem to have no effect. My console traces show that I am clicking on a new item and no errors are being thrown, so I'm a bit stumped at this point.

Any help would be appreciated, thanks.
0 Kudos
LarsHuttar
New Contributor III
@huttarl
I am having the same issue as setExtent not working and i tried converting the string to float as you mentioned but no luck.What i am actually trying is when they click save ,it should refresh the map with same location.Please see the attached code.
I am also thinking of version of the script.

Thanks,
Vani, what actually is happening when they click save? nothing?
Actually I don't see a save button in your code. Do you mean the Refresh asp:button?

Have you tried checking the values of xmin etc. after these statements:
        newExtent.xmin =parseFloat(coordinate[1]);
        newExtent.ymin = parseFloat(coordinate[3]);

...
e.g. using an alert, or Firebug console, or something?
I'm curious why you use coordinate subscripts 1,3,5,7 instead of 1,2,3,4.
0 Kudos