Connect to onExtentChange with deferred web map

1025
4
12-17-2012 05:51 AM
GregYetman
Occasional Contributor
Hi,

I am trying to connect to the onExtentChange event of web map (from ArcGIS Online). I have tried two different methods with limited success. The first way I tried (see code below) is only called after the map loads, not after each extent change. The second way is called after the map loads and after every update to the map extents. However, only the first call is successful. The calls after the map loads (triggered by panning the map) give this error:

TypeError: _508.advice is null

from the javascript api. I assume that this is because after the initial load the map object I am passing is null. Any advice on how should I make the connection?

cheers,

Greg


   
<script type="text/javascript">
      dojo.require("esri.map");
   dojo.require("esri.arcgis.utils");
   var map, maxExtent,iniExtent, tiledMapServiceLayer;   

      function init() {
     // configure the proxy URL -- required to access Data not on the local server
  esri.config.defaults.io.proxyUrl = "/proxy/proxy.ashx";
    var lods = [ {"level" : 6, "resolution" : 2445.98490512499, "scale" : 9244648.868618},
               {"level" : 7, "resolution" : 1222.99245256249, "scale" : 4622324.434309},
               {"level" : 8, "resolution" : 611.49622628138, "scale" : 2311162.217155},
               {"level" : 9, "resolution" : 305.748113140558, "scale" : 1155581.108577},];
  
  var webmap = "longwebmapstring"
    iniExtent = new esri.geometry.Extent({"xmin": -7315000, "ymin": 5829000, "xmax": -4967000, "ymax": 6836000, "spatialReference":{"wkid":102100}});
  mapDeferred = esri.arcgis.utils.createMap(webmap, "map", {
          mapOptions: {
      extent:iniExtent,
   lods: lods,
                        slider: true,
   sliderStyle: "small",
                        nav:false
          }});
  mapDeferred.addCallback(function (response) {
   var map = response.map;
   //this line is only called on init
                        //dojo.connect(dijit.byId("map"), "onExtentChange", map, checkExtents(map)); 
                        //this line is called onExtentChange but the map passed is null.
   dojo.connect(map, "onExtentChange", checkExtents(map)); 
  });
 }

     function checkExtents(map) {
   console.log("checkExtents called");
   console.log(map.extent);
  };
  
 dojo.ready(init);
    </script>
0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Greg,

Try the following instead:

dojo.require("esri.map");
dojo.require("esri.arcgis.utils");

var map, maxExtent,iniExtent, tiledMapServiceLayer;      

function init() {
    // configure the proxy URL -- required to access Data not on the local server
    esri.config.defaults.io.proxyUrl = "/proxy/proxy.ashx";
    var lods = [ {"level" : 6, "resolution" : 2445.98490512499, "scale" : 9244648.868618},
                     {"level" : 7, "resolution" : 1222.99245256249, "scale" : 4622324.434309},
                     {"level" : 8, "resolution" : 611.49622628138, "scale" : 2311162.217155},
                     {"level" : 9, "resolution" : 305.748113140558, "scale" : 1155581.108577},];
        
    var webmap = "longwebmapstring"
    iniExtent = new esri.geometry.Extent({"xmin": -7315000, "ymin": 5829000, "xmax": -4967000, "ymax": 6836000, "spatialReference":{"wkid":102100}});
    mapDeferred = esri.arcgis.utils.createMap(webmap, "map", {
        mapOptions: {
            extent:iniExtent,
            lods: lods,
                        slider: true,
            sliderStyle: "small",
                        nav:false
          }});         
        
        mapDeferred.addCallback(function (response) {
            var map = response.map;
            dojo.connect(map, "onExtentChange", checkExtents); 
        });
    }


  
function checkExtents(map){
    console.log("checkExtents called");
    
    var extent = "";
     extent = "XMin: "+ map.xmin.toFixed(4) + " "
           +"YMin: " + map.ymin.toFixed(4) + " "
           +"XMax: " + map.xmax.toFixed(4) + " "
           +"YMax: " + map.ymax.toFixed(4);
           
    console.log(extent);

}
        

dojo.ready(init);
0 Kudos
GregYetman
Occasional Contributor
Hi Jake,

Thanks. I tried the code you suggested but it generates an error on the line:

[INDENT]extent = "Xmin: " + map.xmin.toFixed(4) + " "[/INDENT]


The error is:

[INDENT]TypeError: map is undefined[/INDENT]


I guess that the map object is null but I'm not sure why.

I'm new to dojo and deferred responses, hopefully I haven't missed something obvious here.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
For the dojo.connect statement, be sure you remove the argument when you call the 'checkExtents' function.

Ex:

dojo.connect(map, "onExtentChange", checkExtents);
0 Kudos
GregYetman
Occasional Contributor
Yes, I had been doing that after your previous post, however I was still getting the 'map is undefined' error. Then I realized that the problem could be that the function was being called before the map was loaded, so I modified it based in part on some sample viewers on the Esri site, such as this one:

http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples_start.htm#jssamples/overlays.html

and it started working fine.

Thanks again,

Greg

  mapDeferred.addCallback(function (response) {
   map = response.map;
   if (map.loaded) {
    mapLoaded();
   }
   else {
    dojo.connect(map,"onLoad",mapLoaded);
   }
  });


<snip>

 function mapLoaded() {
  dojo.connect(map, 'onExtentChange', checkExtents);
  };
0 Kudos