AnsweredAssumed Answered

Help with setting extent based on feature layer

Question asked by sbyrne on Sep 25, 2015
Latest reply on Sep 25, 2015 by sbyrne

Hi there. I'm a beginner in the realms of the javascript API. I'm trying to zoom to the extent of a feature layer I've added to my map. When I try to run the code (attached), I don't get an error but the map extent does not change to the extent of the layer. I've checked out the rest service for my feature layer and verified that it is in the same projection as the base map (3857) and that the extent coordinates are what I expect them to be. I can set up the extent manually to those same coordinates and spatial reference and all works fine, but I'd like to set it dynamically based on the layer. Can anyone point me in the right direction?


<!DOCTYPE html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Resource Loader Test</title>

    <link rel="stylesheet" href="">
    <link rel="stylesheet" href="">

  <script src=""></script>

    function (Map, FeatureLayer, Extent, SpatialReference, ready, parser, on) {

        ready(function () {

        var myLyr = new FeatureLayer("//", {
        autoGeneralize: true,
        visible: true,
        opacity: 0.5
        var mapMain = new Map("mapDiv", {
            basemap: "streets",
        extent: myLyr.fullExtent,
        zoom: 5,

        //var startExtent = new Extent(-13887750, 3371860, -10999990, 6275470, new SpatialReference({ wkid:3857 }))
        mapMain.on("extent-change", function(printExtent) {
          curExtent = mapMain.extent
          var extentString = "";
          extentString = "XMin: " + Math.round(curExtent.xmin) +
        ", YMin: " + Math.round(curExtent.ymin) +
        ", XMax: " + Math.round(curExtent.xmax) +
        ", YMax: " + Math.round(curExtent.ymax);
          dojo.byId("mapInfo").innerHTML = extentString;


<body class="claro"> 
  <div id="mapDiv" style="width:900px; height:600px;"></div>
  <div id="mapInfo"></div>