Working with the Popup Widget in the ArcGIS Server API for JavaScript

by | Jun 13, 2011

The new Popup widget, provided with the 2.3 release of the ArcGIS Server API for JavaScript can be used as a replacement for the default info window.  This new widget provides an attractive alternative to the info window complete with navigation tools that allow you to move through the selected features, zoom to a selected feature, highlight the selected feature, and maximize/minimize,close the window.

You can also customize the look of the window as well as the content.  In this post you will learn how easy it is to programmatically add this new widget to your application.  You can also use the ArcGIS.com map viewer to create popup windows.  This post is the first in a three part series.  This first post will cover the basics of adding a Popup window to your application while the second and third posts will cover advanced customization topics.

Reference the Widget
You’ll first want to add the popup stylesheet to your application as seen below:

Next, reference the Popup widget using dojo.require.

Creating the Popup
The constructor for a Popup widget takes two parameters: options and an HTML source reference to where the popup will be placed.  The ‘options’ parameter is optional and can be used to customize the look of the popup window.

In the code example below you’ll see that we simply provide a ‘null’ reference for the options object.  This simply means that we’ll use the default options provided by the Popup.  The srcNodeRef is going to be a <div> container that we create to hold the content.

The second line of code highlighted below indicates that when the map is initially created the new Popup widget, stored in a variable called ‘popup’, will serve as the info window for the application.

Finally, the dojo.place method is used to define where the popup will appear which in this case is the <div> container that we created.  Place the popup under the map’s root element. This ensures that the coordinate space used by the popup for positioning aligns with the map’s coordinate space.

Adding just the lines we’ve already discussed is enough to create a basic Popup window.  Many times that’s all you need for your application, but you can also customize not only the look of the window but also the content.

Formatting with the PopupTemplate Class
Popup content can be formatted using the PopupTemplate class.  PopupTemplate inherits from the InfoTemplate class.  A code example showing how to create an instance of PopupTemplate is provided below.  This object can be used to create a title and description for the window along with a means for defining the fields that should be visible along with their labels and formatting.  You can also define whether attachments should be visible (showAttachments:<boolean>) as well as images, charts, and other media that will be part of the window.  We’ll examine some of these advanced customization techniques in our second post.

In this code example above we’ve simply defined a title for the window along with the fields that will be visible and stated that we want attachments to be loaded if they exist.

A PopupTemplate is then referenced from the ‘infoTemplate’ option when defining a new FeatureLayer as seen in the code example below.

 

Categories

Recent Posts

Eric Pimpler
Eric is the founder and owner of GeoSpatial Training Services (geospatialtraining.com) and has over 25 years of experience implementing and teaching GIS solutions using ESRI, Google Earth/Maps, Open Source technology. Currently Eric focuses on ArcGIS scripting with Python, and the development of custom ArcGIS Server web and mobile applications using JavaScript. Eric is the author of Programming ArcGIS with Python Cookbook - 1st and 2nd Edition, Building Web and Mobile ArcGIS Server Applications with JavaScript, Spatial Analytics with ArcGIS, and ArcGIS Blueprints. Eric has a Bachelor’s degree in Geography from Texas A&M University and a Master's of Applied Geography degree with a concentration in GIS from Texas State University.

Sign up for our weekly newsletter
to receive content like this in your email box.