Select to view content in your preferred language

Setting map extent to dynamic layer's initial extent doesn't always work

1147
4
Jump to solution
07-30-2013 01:15 PM
KenBuja
MVP Esteemed Contributor
I'm setting the map extent to a dynamic layer once it's loaded using the following code.

    layerDynamic.on("load", function () {         var deferred = new Deferred();         deferred = map.setExtent(layerDynamic.initialExtent, true);         deferred.then(function () {             console.log("map xmin: " + map.extent.xmin + ", ymin: " + map.extent.ymin + ", xmax: " + map.extent.xmax + ", ymax: " + map.extent.ymax)             console.log("layer xmin: " + layerDynamic.initialExtent.xmin + ", ymin: " + layerDynamic.initialExtent.ymin + ", xmax: " + layerDynamic.initialExtent.xmax + ", ymax: " + layerDynamic.initialExtent.ymax)         });     });


However, this doesn't always work. When I refresh the page, it often zooms out way too far. This is what the extents usually are when it doesn't zoom correctly

map xmin: -27374674.768619433, ymin: -2290122.0955929635, xmax: -6828401.565569537, ymax: 6241473.253482993 layer xmin: -14166895.9892999, ymin: 5762234.783315, xmax: -13598348.0746001, ymax: 6211946.863385


Why doesn't that code work upon a refresh?
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
This problem was eventually solved by wrapping the map initialization in a setTimeout function. The root of the problem seemed to be that the project has two scripts. The first reads in the configuration file using esriRequest and the second does the map initialization based on values read from the configuration file. It seems the map initialization was happening before the request was finished. Rewriting the code into a single script solved the problem in the end.

View solution in original post

0 Kudos
4 Replies
BenFousek
Regular Contributor
Ken,

initialExtent is the extent of the mxd when published.

Try fullExtent, which is the extent of the data.

map.setExtent(layerDynamic.fullExtent, true);
0 Kudos
KenBuja
MVP Esteemed Contributor
Ken,

initialExtent is the extent of the mxd when published.

Try fullExtent, which is the extent of the data.

map.setExtent(layerDynamic.fullExtent, true);


Thanks Ben, but I do want the initial extent rather than the full extent of the service. And in the case of this particular service, the initial extent is not that much different than the full extent


Initial Extent:

    XMin: -14166895.9892999
    YM in: 5762234.783315
    XMax: -13598348.0746001
    YMax: 6211946.863385
    Spatial Reference: 102100 (3857)
Full Extent:
[INDENT]
XMin: -14015721.4263
YMin: 5782676.2415
XMax: -13749522.6376
YMax: 6191505.4052
Spatial Reference: 102100 (3857)
[/INDENT]
0 Kudos
BenFousek
Regular Contributor
In general, I prefer fullExtent because some 3rd party map services are published with strange extents, e.g. zoomed way in outside the data extent.

I tested this in an app and it worked fine.
vmc.on('load', function() {
  app.map.setExtent(vmc.initialExtent, true);
});


Is the deferred necessary? Using your code didn't work for me.
0 Kudos
KenBuja
MVP Esteemed Contributor
This problem was eventually solved by wrapping the map initialization in a setTimeout function. The root of the problem seemed to be that the project has two scripts. The first reads in the configuration file using esriRequest and the second does the map initialization based on values read from the configuration file. It seems the map initialization was happening before the request was finished. Rewriting the code into a single script solved the problem in the end.
0 Kudos