Select to view content in your preferred language

map.setExtent not setting right values

540
5
Jump to solution
03-13-2012 10:56 PM
MatejSkerjanc
Occasional Contributor
var factor = 0;
  var xtent =new esri.geometry.Extent(xmin-factor, ymin-factor, xmax+factor, ymax+factor)
  map.setExtent(xtent);
 
 
  alert(map.extent.xmin + ',' +  map.extent.ymin + ',' +map.extent.xmax + ',' + map.extent.ymax );


I got this code, and the xtent of a map differs from the extent of xtent. Any practical reason behind it or is my code just faulty?
0 Kudos
1 Solution

Accepted Solutions
HemingZhu
Occasional Contributor III
var factor = 0;
  var xtent =new esri.geometry.Extent(xmin-factor, ymin-factor, xmax+factor, ymax+factor)
  map.setExtent(xtent);
 
 
  alert(map.extent.xmin + ',' +  map.extent.ymin + ',' +map.extent.xmax + ',' + map.extent.ymax );


I got this code, and the xtent of a map differs from the extent of xtent. Any practical reason behind it or is my code just faulty?


You might want to add sr parameter. list this: var xtent =new esri.geometry.Extent(xmin-factor, ymin-factor, xmax+factor, ymax+factor, map.spatialReference);

View solution in original post

0 Kudos
5 Replies
JohnGrayson
Esri Regular Contributor
You are probably reading the values before the map actually changes extent.  I suggest you listen to the map.onExtentChange event and then get the map extent values.

var factor = 0;
var xtent =new esri.geometry.Extent(xmin-factor, ymin-factor, xmax+factor, ymax+factor)

var mapExtentChangeHandle = dojo.connect(map, 'onExtentChange', function(newExtent){
  dojo.disconnect(mapExtentChangeHandle);

  alert(dojo.replace("{xmin},{ymin},{xmax},{ymax}", newExtent);
  alert(map.extent.xmin + ',' + map.extent.ymin + ',' +map.extent.xmax + ',' + map.extent.ymax);
});

map.setExtent(xtent);
0 Kudos
HemingZhu
Occasional Contributor III
var factor = 0;
  var xtent =new esri.geometry.Extent(xmin-factor, ymin-factor, xmax+factor, ymax+factor)
  map.setExtent(xtent);
 
 
  alert(map.extent.xmin + ',' +  map.extent.ymin + ',' +map.extent.xmax + ',' + map.extent.ymax );


I got this code, and the xtent of a map differs from the extent of xtent. Any practical reason behind it or is my code just faulty?


You might want to add sr parameter. list this: var xtent =new esri.geometry.Extent(xmin-factor, ymin-factor, xmax+factor, ymax+factor, map.spatialReference);
0 Kudos
MatejSkerjanc
Occasional Contributor
I've used the SR it didn't help thank you though.

I'll go with jgraysons and see if his help.

I'll report back guys thank you for now.
0 Kudos
MatejSkerjanc
Occasional Contributor
You are probably reading the values before the map actually changes extent.  I suggest you listen to the map.onExtentChange event and then get the map extent values.



I'm creating a group of features common extent. Getting minx, miny, maxx, maxy from whole group and assigning it to map extent, so it zooms in/out on the whole group. And the group extent comes out just fine. The problem is the map.setExtent sets extent to different envelope
0 Kudos
MatejSkerjanc
Occasional Contributor
Found sollution which was pretty obvious




var xtent = new esri.geometry.Extent({
        "xmin":xmin,
        "ymin":ymin,
        "xmax":xmax,
        "ymax":ymax,  "spatialReference": { "wkid":map.spatialReference.wkid }
  });

map.setExtent(xtent, true); //When true, for maps that contain tiled map service layers, you are guaranteed to have the input extent shown completely on the map. (As of v1.3)
I did test this but before when my SR was wrong and it "crashed with error". Previously i had SR set to   "spatialReference": { "wkid":map.spatialReference } and it's wrong it seems.



Thank you guys
0 Kudos