Welcome to ESRI Blogs

Extending tiled layers in the ArcGIS API for Flex

The ArcGIS API for Flex supports access to layer types not included with the API. Depending on what you are trying to connect to (or do), you might want to extend TiledMapServiceLayer, DynamicMapServiceLayer or Layer.

  • Extend DynamicMapServiceLayer if you want to connect to a service that returns one complete image, usually created on the fly (for example, WMS).
  • Extend TiledMapServiceLayer if you want to connect to some system of (usually pre-created and cached) tiles (for example, an ArcGIS Server virtual cache directory). This is especially useful if you want to access 9.2 services, since 9.2 doesn't have REST support and thus isn't supported using the out-of-the-box ArcGIS API for Flex.

Today I will show how to create an example application that extends TiledMapServiceLayer to access tiles created in either ArcGIS 9.2 or 9.3 and which have been made available on the Web server (usually /arcgiscache/).

 Example Flex application that extends TiledMapServiceLayer

Extending TiledMapServiceLayer follows the same inheritance as the out-of-the-box ArcGISTiledMapServiceLayer.

In Flex API: ArcGISTiledMapServiceLayer -> TiledMapServiceLayer -> Layer -> mx.core.UIComponent

This sample: PortlandTiledMapServiceLayer -> TiledMapServiceLayer -> Layer -> mx.core.UIComponent

For simplicity, in this sample, we will hard-code configuration settings such as extents and spatial reference. This is a little different from the out-of-the-box ArcGIS layers which retrieve these settings from the REST Services Directory.

Tutorial

This tutorial shows how to extend tiled layers in the ArcGIS API for Flex to support tiles available outside the REST endpoint. The live application and its source code are also available.

Follow these three steps in Flex Builder:

  1. Create new ActionScript class to extend the TiledMapServiceLayer class
    • Add a new ActionScript class
    • Set the package to "com.esri.ags.samples"
    • Set the name to "PortlandTiledMapServiceLayer"
    • Set the superclass to "com.esri.ags.layers.TiledMapServiceLayer"
    • Click Finish
    This will create PortlandTiledMapServiceLayer.as in com/esri/ags/samples/.
  2. Edit the new PortlandTiledMapServiceLayer class
    • Override the required TiledMapServiceLayer functions, fullExtent() and tileInfo() and have them return the specified values, for example:
      override public function get fullExtent():Extent
      {
      return new Extent(-123.596895130725,
      44.297575737946,
      -121.553757125519,
      46.3683237161949,
      new SpatialReference(4326));
      }
    • Override the TiledMapServiceLayer getTileURL() function to give us the appropriate tiles and have the map place them in the right place.
      private var _baseURL:String = "http://sampleserver1.arcgisonline.com/arcgiscache/Portland_Portland_ESRI_LandBase_AGO/Portland/_alllayers";
      override protected function getTileURL(level:Number, row:Number, col:Number):URLRequest
      {
      var url:String = _baseURL
      + "/L" + padString(String(level), 2, "0")
      + "/R" + padString(row.toString(16), 8, "0")
      + "/C" + padString(col.toString(16), 8, "0") + ".jpg";
      return new URLRequest(url);
      }
    • Inside the public function PortlandTiledMapServiceLayer(), add
      buildTileInfo(); // to create our hardcoded tileInfo
      setLoaded(true); // Map will only use loaded layers
    • Optionally, as a best practice, similarly override the following properties from the Layer class: initialExtent, spatialReference and units. This is needed if this is the first layer added to the map and the map property hasn't been set yet. For example:
      override public function get initialExtent():Extent
      {
      return new Extent(-122.539, 45.500, -122.540, 45.501, new SpatialReference(4326));
      }
  3. Use it with either MXML or ActionScript.
    <esri:Map id="myMap">
    <samples:PortlandTiledMapServiceLayer id="virtualTiles" fadeInFrameCount="15"/>
    </esri:Map>
    or actionscript
    var myMap:Map = new Map;
    this.addChild(myMap);
    var myLayer:PortlandTiledMapServiceLayer = new PortlandTiledMapServiceLayer();
    myMap.addLayer(myLayer);

Note: If the web server hosting the tile images doesn't have a crossdomain file your application will still work, but while developing you will see a warning ("Warning: Failed to load policy file"), as well as security sandbox violations for each tile.

Resources

This shows how easy it is to extend a layer to add support for additional types of layers within the ArcGIS API for Flex.

Contributed by Bjorn Svensson of the ArcGIS API for Flex team.

Published Thursday, March 05, 2009 4:19 PM by sterlingdq

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Extending tiled layers in the ArcGIS API for Flex

Great post. Could you show us how to implement this to use WMS? I think a lot of people would find this useful. Thanks Matt
Friday, March 06, 2009 3:10 AM by Matt B

# re: Extending tiled layers in the ArcGIS API for Flex

Brilliant, thanks!
Monday, March 09, 2009 2:12 PM by Brian

# re: Extending tiled layers in the ArcGIS API for Flex

it is very cool. Could you show us how to implement this to use OpenLayer?
Sunday, March 15, 2009 7:26 AM by shenyu

# re: Extending tiled layers in the ArcGIS API for Flex

Could you give any guidance on how to set it up to read the conf.xml file so that you wouldn't have to set the parameters for the layer? Thanks, George
Thursday, March 19, 2009 5:51 PM by George

# re: Extending tiled layers in the ArcGIS API for Flex

Matt et al, at the Developer Summit we showed how to extend DynamicMapServiceLayer to access WMS services. The code from that presentation is available in the Code Gallery: http://resources.esri.com/arcgisserver/apis/flex/index.cfm?fa=codeGalleryDetails&scriptID=16176
Wednesday, May 27, 2009 3:43 PM by Bjorn Svensson

Leave a Comment

(required) 
required 
(required)