set extent while adding dynamic layer to base map

5035
4
03-03-2015 10:37 AM
BhavinSanghani
Occasional Contributor II

I am trying to add published map service as a dynamic layer on the base map. If I give extent: fullextent or initialExtent available on mapservice then the map is not displayed at sufficient zoom level.

For example, I am trying to add following mapservice as a dynamic layer to the base map. But the output is not shown at sufficient zoom level.

RoadsHighways/NewYorkRoads (MapServer)

Let's say if you try to open this mapservice in ArcGIS Javascript Viewer (available on the given link) then it shows proper zoom level. But if you use ArcGIS.com map viewer, then it shows it at different zoom level.

It looks like bbox values are calculated when ArcGIS Javascript Viewer is used. Is there any way to calculate it dynamically?

In my case, I can't hardcode extent values when I instantiate map object. Because my customers can provide any mapservice in my code and accordingly I need to show it on the map. My design decision is - whatever extent customer has set it up in ArcMap before publishing the map service, I will use that to show the map.

Any suggestions to show dynamic layer at sufficient zoomed level when added to the basemap? Also, suggest if there is a better design decision.

0 Kudos
4 Replies
KenBuja
MVP Esteemed Contributor

When using this layer with a tiled basemap, you cannot set the extent of the map to the exact extent of the layer. The tiled basemap will dictate what the levels of detail are.

You can set the extent of the map from the layer's information. You have the option of using the layer's full extent or initial extent

map.setExtent(myLayer.fullExtent, true);
map.setExtent(myLayer.initialExtent, true);
0 Kudos
BhavinSanghani
Occasional Contributor II

See the following code. What is the best practice in this scenario to set map extent? I tried fullextent and initialextent but it doesn't seem to work.

<!DOCTYPE html>

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <!--The viewport meta tag is used to improve the presentation and behavior of the samples

      on iOS devices-->

    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">

    <title>Create Map and add a dynamic layer</title>

    <link rel="stylesheet" href="http://js.arcgis.com/3.12/esri/css/esri.css"/>

    <style>

      html, body, #mapDiv{

        padding: 0;

        margin: 0;

        height: 100%;

      }

    </style>

    <script src="http://js.arcgis.com/3.12/"></script>

    <script>

      var map;

      require([

        "esri/map",

        "esri/layers/ArcGISDynamicMapServiceLayer",

        "esri/layers/ImageParameters",

  "esri/geometry/Extent"

      ], function (

        Map, ArcGISDynamicMapServiceLayer, ImageParameters, Extent) {

        var imageParameters = new ImageParameters();

        imageParameters.format = "PNG24"; //set the image type to PNG24, note default is PNG8.

        //Takes a URL to a non cached map service.

  var vDynamicLayerUrl = 'http://roadsandhighwayssample.esri.com/arcgis/rest/services/RoadsHighways/NewYorkRoads/MapServer';

        var dynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer(vDynamicLayerUrl, {

          "opacity" : 0.5,

          "imageParameters" : imageParameters

        });

//This works great! But I don't know what extent values customer wants.

  /*

  var vExtent = new Extent({

  "xmin": -8779565.491,

  "ymin": 5308898.15,

  "xmax": -8768418.742,

  "ymax": 5315478.761,

  "spatialReference": {

  "wkid": 102100

  }

  });

  */

        map = new Map("mapDiv", {

          basemap: 'gray',

  //extent: vExtent,

          sliderOrientation : "horizontal"

        });

        map.addLayer(dynamicMapServiceLayer);

  map.on('layer-add-result', function(dynamicLayer) {

//following code doesn't show dynamic layer on the map or may be taking time to add the layer. Also, it shows me the following message on the console.

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

  //map.setExtent(new Extent(dynamicLayer.layer.initialExtent), true);

  map.setExtent(new Extent(dynamicLayer.layer.fullExtent), true);

  });

      });

    </script>

  </head>

  <body>

    <div id="mapDiv"></div>

  </body>

</html>

0 Kudos
KenBuja
MVP Esteemed Contributor

The issue was in the spatial reference of the dynamic service. Since it's not either a web mercator or a geographic, you have to use a geometry service to project the extent into the map's spatial reference.

I've made a few changes to your code here: JS Bin - Collaborative JavaScript Debugging

BhavinSanghani
Occasional Contributor II

Thanks Ken for your reply!

I tried the way you suggested and I am able to show dynamic layer with the basemap. But when I am trying to add any GraphicsLayer on top of the map, it doesn't display.

GraphicsLayer has spatialReference {wkid: 4326} set automatically, I am not sure where it comes from. DynamicServiceLayer has spatialReference: {wkid: 102719}. If I try to highlight feature using FeatureLayer - selectFeatures() then it should display in North Carolina as per wkid but it displays in Africa somewhere.

If I don't show the basemap then no issues with highlighting features. Does that mean I have to do reprojection every time I add any type of the layer on the map? I tried that even but no success and following message appears on the console.

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

wkid:102100 is of basemap: streets.

What is the best practice for such scenarios? Or using basemap and dynamiclayer with different co-ordinate system itself is not a best practice?

0 Kudos