Working with the Map Extent – ArcGIS Server JavaScript API

Posted on August 16, 2008. Filed under: ESRI |

Continuing with our series on the ArcGIS Server JavaScript API, today we’ll cover various ways that you can work with the map extent.  If you don’t include extent information when an instance of the map is created in your code, the default or initial extent will be the extent of the map as it was last saved in the map document (.mxd).  For example, the code snippet below does not include any extent information in the constructor for the map.  This would result in a default extent being applied which was the extent of the map when it was last saved in the map document.

Here is the result.

In the event that you are using more than one service, the default extent will be the initial extent of the base map or first layer added.

Setting the Initial Extent
There are several options for setting the initial map extent through your code.  The easiest way to do so is through the constructor for the map.  The constructor for the Map object takes a required parameter which points to <div> element on your page, and an options parameter which can contain the initial extent of the map as well as other options such as the layer used to initialize the map, the inclusion of navigation controls, and others.  Click here to see a full description of the available options that can be included in the constructor.

For instance, the following code snippet shows how you can accomplish setting the extent through the constructor.   To set the initial extent of the map you will need to know the min and max x and y values of the extent.

You can also accomplish the same task but in a slightly different manner through the use of the setExtent method on Map as you see in the code example below.

You could also set each property of the extent separately.

Here is the result.

Setting the Initial Extent when using Multiple Services
If you’re using multiple map services in your application, the default extent will be the initial extent of the base layer.  If you know the extent you’d like to use ahead of time then you can include the extent in the map constructor as we’ve shown above.  However, at times you will either not know the extent ahead of time or you may want to use the extent of a secondary layer.  For instance, if you’re using a map service that shows the extent of the world as your base layer, and a second map service with your local data you’d probably want to set the extent of your application to that of your local data.  The code example below from ESRI shows how this can be accomplished through the fullExtent property found on ArcGISMapServiceLayer.

Getting the Current Extent
You can obtain the current map extent through the read-only Map.extent property or through the use of events.  The later method for returning the current map extent is through the use of Map events, and specifically the “onExtentChange” event.  The topic of events requires a fair amount of explanation so I will save this for my next post, but for now here is an example which uses the “onExtentChange” event to return the current map extent which is then displayed in a <div> tag below the map.

Make a Comment

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: