center map on layer extent

4350
10
Jump to solution
01-13-2014 08:23 AM
MaxDemars
New Contributor III
Hello,

Im new to the arcgis javascript api and I cant find answer to my question. esri.map seems to only have centerAt(mapPoint) method but how to center the map on a ArcGISDynamicMapServiceLayer extent not on a point?

Thank you
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
Try this instead

require(["esri/map", "dojo/domReady!", "esri/layers/ArcGISDynamicMapServiceLayer", "esri/SpatialReference",], function(Map) {     var layer1 = new esri.layers.ArcGISDynamicMapServiceLayer("http://localhost:6080/arcgis/rest/services/carte1/MapServer");     map = new Map("maptest1");     map.addLayers([layer1]);      map.on("layers-add-result", function(){         var extent = layer1.fullExtent;         map.setExtent(extent);         alert(extent.xmax);     }); }); 

View solution in original post

0 Kudos
10 Replies
JohnGravois
Frequent Contributor
welcome to the forums burton!

if you have a reference to an extent object, you could find its center using the method extent.getCenter()

afterward, you could pass the point which is returned to map.centerAt()
0 Kudos
MaxDemars
New Contributor III
welcome to the forums burton!

if you have a reference to an extent object, you could find its center using the method extent.getCenter()

afterward, you could pass the point which is returned to map.centerAt()


Allright, I understand that, the map center is moved to the center of the layer, but the map is not zoomed to the layer extent.
0 Kudos
JohnGravois
Frequent Contributor
you probably want to be calling map.setExtent().
0 Kudos
MaxDemars
New Contributor III
Thank you for your help jgravois,

The problem I encounter is that the layer I am trying to zoom to, an esri.layers.ArcGISDynamicMapServiceLayer instance, do not have a getExtent() method and the fullExtent property is empty. By default the map extent is set in order that we only see a part of the layer, but not its entire extent.
0 Kudos
KenBuja
MVP Esteemed Contributor
You may be checking the fullExtent property before the layer is ready. Try getting the full extent in the layer's load event.
0 Kudos
MaxDemars
New Contributor III
Hi kenbuja,

I dont know what is wrong here... fullExtent is always empty

      require(["esri/map", "dojo/domReady!", "esri/layers/ArcGISDynamicMapServiceLayer", "esri/SpatialReference",], function(Map) {
[INDENT]           var layer1 = new esri.layers.ArcGISDynamicMapServiceLayer("http://localhost:6080/arcgis/rest/services/carte1/MapServer");
           extent = layer1.fullExtent
           alert(extent) // empty box is popped
           map = new Map("maptest1");
           map.addLayers([layer1]);
           map.setExtent(extent);[/INDENT]
      });
0 Kudos
KenBuja
MVP Esteemed Contributor
Try this instead

require(["esri/map", "dojo/domReady!", "esri/layers/ArcGISDynamicMapServiceLayer", "esri/SpatialReference",], function(Map) {     var layer1 = new esri.layers.ArcGISDynamicMapServiceLayer("http://localhost:6080/arcgis/rest/services/carte1/MapServer");     map = new Map("maptest1");     map.addLayers([layer1]);      map.on("layers-add-result", function(){         var extent = layer1.fullExtent;         map.setExtent(extent);         alert(extent.xmax);     }); }); 
0 Kudos
JohnGravois
Frequent Contributor
as ken said, the call to the service itself is asynchronous so you have to wait until you know the layer has actually completed loading.

map.addLayer(dynamicMapServiceLayer);
console.log(dynamicMapServiceLayer.fullExtent); //this will be null
dynamicMapServiceLayer.on("load", function(evt) {
  console.log(evt.layer.fullExtent);   //this will be populated       
});
0 Kudos
MaxDemars
New Contributor III
Try this instead

require(["esri/map", "dojo/domReady!", "esri/layers/ArcGISDynamicMapServiceLayer", "esri/SpatialReference",], function(Map) {
    var layer1 = new esri.layers.ArcGISDynamicMapServiceLayer("http://localhost:6080/arcgis/rest/services/carte1/MapServer");
    map = new Map("maptest1");
    map.addLayers([layer1]);

    map.on("layers-add-result", function(){
        var extent = layer1.fullExtent;
        map.setExtent(extent);
        alert(extent.xmax);
    });
}); 



Thank you for your help kenbuja, this is working correctly. I will have to study events carefully as it seems its the base of this API.
0 Kudos