[GIS] ArcGIS JavaScript API: Does current extent match initial extent

arcgis-javascript-apiextents

In using the JS API, I need to detect if the current extent matches the initial extent. If no match, then I need to display a UI element ("Return to Original Extent").

The initial extent is cached in a data store after the map loads (in my own object mapState.initialExtent). This is properly configured as an instance of an esri extent. I can trigger the map to move to this extent successfully.

I have created a listener on the map (onExtentChange) which calls a function which compares the current extent to the initial extent. It seems the best way to do this is to compare the x/y min/max which is fine and works EXCEPT that BOTH the return from the call to map.extent AND the extent return by the listener are inaccurate.

That is: if I do this:

// SET LISTENER ON MAP LOAD

dojo.connect(map, "onExtentChange", compareExtent)

// AT SOME TIME LATER DO THIS
// this works
map.setExtent(mapState.initialExtent) 

function compareExtent(extent, delta, levelChange, lod) {

// FAILS WHEN COMPARING THE FOLLOWING:
 var currentExtent = map.extent;
 var returnedExtent = extent;
 var initialExtent = mapState.initialExtent;

console.log("currentExtent.xmin", currentExtent.xmin, "initialExtent.xmin",        initialExtent.xmin, "extent.xmin", extent.xmin);
console.log("currentExtent.ymin", currentExtent.ymin, "initialExtent.ymin", initialExtent.ymin, "extent.ymin", extent.ymin);

//these do not match
//HERE IS THE CONSOLE READ OUT
currentExtent.xmin -8476373.507203963 
initialExtent.xmin -8476287.515547141 
extent.xmin -8476373.507203963

currentExtent.ymin 4412584.280379601 
initialExtent.ymin 4412589.057693868 
extent.ymin 4412584.280379601

}

So, I checked to see if this listener actually returns BEFORE the map is finished but it does not.

Anyone have ideas?


Edit

I am sorry this is not clear. I set the map to the initial extent, change the extent in response to a UI event and then return to the initial extent: the values do not match though the map appear to be correct.

To be clear, on map init I set the extent to initialExtent – using setExtent(initialExtent) where initialExtent is a cached instance of an extent. I then go to a different cached extent and later return to initialExtent. The console shows the returns.

The value currentExtent.xmin -8476373.507203963 is what is returned by the listener.

The value initialExtent is what is initially cached (which is passed as the argument in both the first and the final setExtent function – – this value is initialExtent.xmin -8476287.515547141.

The following value is what is returned by map.extent after the listener fires after setExtent(initialExtent):extent.xmin -8476373.507203963

I expect these 3 values to be same.

Best Answer

The differences may be due to the map levels. If you have a tiled service that defines the LODs of the map, when you set the initial extent it will actually adjust to the closest LOD (map.level) that can match the given initialExtent you provided. I'm not completely sure, but it's something that has bitten me before.