Select to view content in your preferred language

Setting map extent to extent of a ArcGISDynamicMapServiceLayer

3562
6
07-03-2013 08:49 AM
baileyhanson
Deactivated User
I am having difficulty figuring out how to set the map extent to that of a dynamic layer. Can anyone point me in the right direction?
0 Kudos
6 Replies
baileyhanson
Deactivated User
I just tried this but it's not changing anything...


var extent = new esri.geomentry.Extent();
  extent.xmin = 256450.78762439353;
  extent.ymin = 4497518.370302271;
  extent.xmax = 643092.2901218915;
  extent.ymax = 4634966.500851203;
  spatialReference = 26915;
0 Kudos
NicholasKnabe
Emerging Contributor
          var initExtent = new esri.geometry.Extent({
              "xmin": -102.03,
              "ymin": 33.39,
              "xmax": -101.65,
              "ymax": 33.8,
              "spatialReference": {
                  "wkid": 4326
              }
          });


          map = new esri.Map("map", {
              extent: esri.geometry.geographicToWebMercator(initExtent)
          });
0 Kudos
KenBuja
MVP Esteemed Contributor
Here's an example of zooming to the extent of a dynamic map service, using AMD code

<!DOCTYPE html>
<html>
<head>
  <title>Create a Map</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10">
  <link rel="stylesheet" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/dojo/dijit/themes/claro/claro.css">
  <link rel="stylesheet" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/esri/css/esri.css">
  <style>
    html, body, #mapDiv{
      padding: 0;
      margin: 0;
      height: 100%;
    }
  </style>
  
  <script src="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/"></script>
  <script>
    var map, layerDynamic;
    require(["esri/map", "dojo/Deferred", "dojo/domReady!"], function(Map, Deferred) {
      map = new Map("mapDiv", {
        center: [-56.049, 38.485],
        zoom: 3,
        basemap: "streets"
      });
      layerDynamic = new esri.layers.ArcGISDynamicMapServiceLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Louisville/LOJIC_LandRecords_Louisville/MapServer", {
        id: 'Dynamic',
      });
      map.addLayers([layerDynamic]);
      
      
      layerDynamic.on("load", function () {
        var deferred = new Deferred();
        deferred = map.setExtent(layerDynamic.initialExtent, true);
        deferred.then(function () {
            console.log("Extent set");
        });
    });
    });
  </script>

</head>
<body class="claro">
  <div id="mapDiv"></div>
</body>
</html>

0 Kudos
baileyhanson
Deactivated User
Ken, shouldn't I be able to use this code and replace the ArcGISDynamicMapServiceLayer URL with my URL? It still won't zoom to the layer.
0 Kudos
KenBuja
MVP Esteemed Contributor
It looks like it has difficult with spatial references other than 102100 and 4326. TUsing a map service with another one gives the following message in the console:

Map: Geometry (wkid: 4269) cannot be converted to spatial reference of the map (wkid: 102100)

What you'll have to do is get the extent of the extent of your dynamic layer and project it into 102100 or 4326 using a geometry service. You can then use the projected geometry to set the map's extent.
0 Kudos
baileyhanson
Deactivated User
Thanks for the help. I ended up just using this

var map = new esri.Map("mapDiv", {
        extent: new esri.geometry.Extent({
            xmin: -11068326.4061,
            ymin: 4920683.9772,
            xmax: -10034349.7679,
            ymax: 5501319.0439,
         spatialReference: {
              wkid: 102100
            }
          })
        });
0 Kudos