Section7Exercise1 Use JavaScriptToCustomizeAWebApp
Section7Exercise1 Use JavaScriptToCustomizeAWebApp
Guided Exercise
4
02/2017
Do-It-Yourself Geo Apps MOOC
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 wont 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. Lets begin!
Below the landing image, click JavaScript API and, from the drop-down list, select
Documentation.
Click the Sample Code tab.
In the list of code samples organized by topic on the left side of the page, click Mapping
and Views.
Click Load a WebMap, and read the description below the map.
This component was created in the <body> section of the web page.
Its okay if you dont 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.
At the top of the code document, you see the following components:
2. The title of the web page, which is shown in the title frame of your app.
3. Internal CSS.
4. Links to external CSS stylesheets, or documents that have information used to style
different elements in a web page. We are not going to cover CSS in depth, but if you are
interested in learning more, you can start here.
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 modules 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:
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, lets 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.
The web map ID is a unique set of characters stored in ArcGIS Online that is associated with a
web map. When you are looking at a web map in the Map Viewer in ArcGIS Online, you can see
the web map ID in the URL (as shown in the following graphic). To humans, this map is the
Vision Zero Pedestrian Hazards web map, but to computers, it is the
df458c1c4c7b40a8a38357c8af318ea7 web map.
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!?
In this section, we are going to be talking a lot about classes and properties, which are often
classes themselves. Remember those modules we loaded earlier? Each of those modules has a
group of classes and subclasses within them. A class is a bit of code that returns something
when you pass values to it.
We wont get into too much detail, but for illustrative purposes, lets look at the MapView code
again.
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.
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 Esris 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, lets 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 Searchs entry in the API Reference.
We dont 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 developers best friend!
Lets 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
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 IDs so that the Washington D.C. Vision
Zero Pedestrian Hazards layer is displayed in the app preview, as shown in the graphic below.
In the editor pane, click after the semi-colon on line 52 and, on your keyboard, press
Enter (Return for Apple computers).
Highlight the code from lines 54 to 61, as shown in the graphic below:
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.
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).
Note: Type the entry as indicated above, instead of copying and pasting.
On line 30, add a comma after WebMap and type Search, as shown in the graphic
below.
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.
Copy and paste (or type) the text Lincoln Memorial in the Search widget, and then click
the magnifying glass icon.
From the results drop-down list, choose the first result.
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!
Resources
HTML
http://www.w3schools.com/html/
https://www.codecademy.com/learn/web
CSS
http://www.w3schools.com/css/
https://www.codecademy.com/learn/web
JavaScript
https://www.codecademy.com/learn/javascript
https://www.codeschool.com/learn/javascript
https://teamtreehouse.com/learn-to-code/javascript
http://javascriptplayground.com/
http://www.codeavengers.com/
http://www.codewars.com/
http://coderbyte.com/