Section7Exercise1 Use JavaScriptToCustomizeAWebApp
Section7Exercise1 Use JavaScriptToCustomizeAWebApp
Guided Exercise
Use JavaScript to Customize a Web
App
Section 7 Exercise 1
08/2017
Do-It-Yourself Geo Apps MOOC
Time to complete
Approximately 40-45 minutes.
Introduction
What is an API? API is short for Application Programming Interface and is a term that can take
many forms. The ArcGIS API for JavaScript is a body of code and documentation that you can
use to embed many functions into your web apps, including web maps from ArcGIS Online
and mapping tools that perform querying, analysis, and routing. Most of the configurable
apps we see in other sections of this course rely heavily on the ArcGIS API for JavaScript.
What is JavaScript? JavaScript is a programming language that is used with HTML and CSS to
create web apps. HTML defines the content of web pages, CSS their layout and style, and
JavaScript their behavior (for example, what happens when you click on a particular area of a
web page).
We won't get into a lot of detail about HTML, CSS, and JavaScript in this exercise, but there
are loads of resources available for those of you who are new to web development. Look at
the Resources section at the end of the exercise if you would like to explore these topics
more.
To introduce you to custom app development and the ArcGIS API for JavaScript, we are
going to look at a sample web app, and add some simple HTML and JavaScript to change
the displayed elements and behavior of the web page. Let's begin!
Let's Get Started!
First, you will need to find a sample app to use as the basis for your exploration.
b Below the landing image, click APIs & SDKs and, from the drop-down list, select
JavaScript.
d In the list of code samples organized by topic on the left side of the page, click Mapping
and Views.
e Click Load a WebMap, and read the description below the map.
You will see the HTML and JavaScript code in the editor pane on the left and the app preview
on the right. After you have made edits to your code, you can click Refresh to refresh the
preview.
The app previewed is pretty basic, it simply displays a web map.
This component was created in the <body> section of the web page.
Hint: Scroll down in the editor pane to view the <body> section.
It's okay if you don't understand HTML. You just need to know the basics about tags. Tags are
keywords surrounded by angle brackets, like this: <keyword>. They tell the browser what the
content within them is and, in some cases (like with <body> and <div> tags), how to display
content. In this code, the <div> tags and its attribute, id, is telling the browser to display the
web map.
The first script tag in the graphic above is the API source code (the hosting location of the
API), which you need to access the code in the ArcGIS API for JavaScript. The src attribute is
the URL where this code is accessed.
The second script tag lists specific modules individually. A module is just a bit of code that
allows you to do a specific thing or set of things. For example, "esri/views/MapView" is a
module that has code that you use to render web maps from ArcGIS Online in 2D. You can
find a quick tutorial on how to use the MapView module here.
Hint: All the code you need to build the application is at the end of the tutorial. If you
want to try the tutorial from start to finish, open up the sandbox with the finished code,
delete the code in the editor pane, and follow the tutorial from Step 1. Click Refresh every
time you want to preview the code you have written. Look to the complete code at the
end of the tutorial for guidance if you get stuck.
There is documentation on all of the modules in the API for JavaScript in the API Reference.
Most modules have an alias, or common name, that allows you to access the module quickly
without typing in the full path. (For example, you can type "MapView" in your code rather
than "esri/views/MapView".) These aliases are defined as arguments in the callback function
that runs after all the source code from the JavaScript API has been downloaded. If you want
a fuller explanation, check out this blog. This knowledge is not necessary to perform the
exercise.
You can add any alias in the function, but it is a best practice to use the preferred aliases. You
can find out the preferred alias for a module from the module's entry in the JavaScript API
Reference. In the graphic below, the preferred alias for the MapView module is circled.
The code in the following graphic creates a WebMap instance from the ID of an existing web
map in ArcGIS Online (1) and displays that web map in 2D (2). Does anything look familiar?
The code uses the module aliases from the code above!
Item 2 in the graphic above creates a variable, view, which uses the module MapView:
var view = new MapView({
map: webmap,
container: "viewDiv"
});
If "esri/views/MapView" and the alias MapView above had not been loaded, the map would
not display!
Again, you do not need to understand everything about the code. Just know that you need to
load modules from the ArcGIS API for JavaScript in order to access that code and do things
with it in your web app. If you want to know more, there are great HTML, CSS, and JavaScript
tutorials out there, as well as specific tutorials for the ArcGIS API for JavaScript once you have
mastered the basics.
Now, let's make some changes to the code to see some of the things it can do. First, you are
going to change the web map in your app using the web map ID.
You are going to change the map from the Accidental Deaths map to the Vision Zero
Pedestrian Hazards map using the web map ID.
Assigning a new web map ID for the WebMap instance has changed the web map that is
being rendered in the app. Pretty cool, huh!?
Here, we used the MapView class to display a web map in 2D. Here, a map property and a
container property is passed to the view object.
Note: viewDiv is an ID selector that was defined earlier in the script that basically says the
MapView will render in the entire browser space (height: 100%, width: 100%). View the
graphic below to see how viewDiv is defined.
a Open a new browser window or tab and look at the documentation for the Search widget
in the API Reference.
The Search widget allows the user of our app to search for an address using a locator service
like Esri's World Geocoding Service (the default) or for features using queries against a feature
service (a slightly more advanced topic).
Below the Constructors section for every class you will find the Property Overview, which lists
all the properties that can be passed to the class referenced, and a Property Details section
which provides more information and links to other resources for the properties listed in the
Property Overview.
You will see a long list of properties that can be passed to objects of the Search class, but
rather than go through all of these, let's take a look at one from the Example code.
Here, searchWidget is only being passed one property, view, which sets the Search widget to
a specific view. If you want more information about the view property you can look it up in the
Property Overview or Property Details section of Search's entry in the API Reference.
We don't need to dig any deeper into the API reference for this exercise. Just know that there
is a TON of information there, and with a little patience and experimentation you can learn to
create some pretty amazing custom geo apps. Documentation is a developer's best friend!
Let's add the Search widget to the Load a Basic WebMap sample app we were working with
earlier using the example code from the API Reference.
Step 5: Add the Search widget to the Load a Basic WebMap sample
a Copy all of the code from the Example.
b In another tab, open the Load a Basic WebMap sample you were working on previously.
Note: Make sure you have switched out the web map ID's so that the Washington D.C.
Vision Zero Pedestrian Hazards layer is displayed in the app preview, as shown in the
graphic below.
c In the editor pane, click after the semi-colon on line 52 and, on your keyboard, press
Enter (Return for Apple computers).
d Paste the code copied from the Example on to line 53 in the editor pane.
e Highlight the code from lines 54 to 61, as shown in the graphic below:
f On your keyboard, press Tab three times so that the code you pasted is in proper
alignment with the rest of the code sample, as shown in the graphic below.
At this point you have created an object of the class Search, passed a view property which is
set to the MapView object instantiated on line 49, and added the Search widget to the
MapView in the top left corner on lines 58 and 59. In order for the Search widget to be added
though, you need to load the Search module.
g In the editor pane, click just to the right of the comma on line 26 and, on your keyboard,
press Enter (Return on Apple computers).
i On line 30, add a comma after WebMap and type Search, as shown in the graphic below.
j Click Refresh.
In the preview window, you should see the Search widget appear in the top-left corner of the
app, as shown in the graphic below.
k Copy and paste (or type) the text Lincoln Memorial in the Search widget, and then click
the magnifying glass icon.
Conclusion
Congratulations! You just customized a web app while learning a thing or two about the API
Reference along the way. We are the first to admit that developing is not always intuitive, but
it is not magic either. There are many resources out there. With a little time and dedication,
you can create powerful geo apps, customized to your tastes and needs!