Lesson 5: Adding Layers
Lesson 5: Adding Layers jed124Overview
Overview jed124So far in the course, you've spent the bulk of your time learning about the various web technologies that are involved in the creation of even the simplest web map applications. The last project deliverables were fairly basic, and you're probably itching to create apps that involve layers of your own data. In Lesson 5, we'll see how to create apps that incorporate web maps configured in ArcGIS Online and how to add various types of data layers to a map using Esri's Maps SDK for JS. We'll finish with a discussion of some coding tips now that you've gotten your feet wet with web app development.
Objectives
At the successful completion of this lesson, students should be able to:
- incorporate web maps (like in Lesson1) and web scenes into a JS app;
- use autocasting to avoid explicitly importing certain modules;
- understand how the REST web services protocol works;
- add various data layer types to a map;
- employ strategies to debug apps that aren't working properly;
- identify features in integrated development environments (IDEs) that aid in the coding process.
Questions?
If you have any questions now or at any point during this week, please feel free to post them to the Lesson 5 Discussion Forum. (That forum can be accessed at any time by clicking on the Discussions tab.)
Checklist
Checklist jed124Lesson 5 is one week in length. (See the Calendar in Canvas for specific due dates.) To finish this lesson, you must complete the activities listed below. You may find it useful to print this page out first so that you can follow along with the directions.
| Step | Activity | Access/Directions |
|---|---|---|
| 1 | Work through Lesson 5. | Lesson 5 |
| 2 | Complete the three-part project on the last page of the lesson.
| Follow the directions throughout the lesson and on the Assignment page. |
| 3 | Take Quiz 5 after you read the online content. | Click on "Lesson 5 Quiz" to begin the quiz. |
5.1 Incorporating Web Maps and Web Scenes
5.1 Incorporating Web Maps and Web Scenes jed124Back in Lesson 1, you created a web map of the Jen & Barry’s scenario data in ArcGIS Online, then incorporated that web map into an app using some Esri app building options that require no coding: embedding within a website, using a configurable app template, and using Experience Builder. It turns out that even if you are coding your app, it’s quite easy to incorporate web maps like the one from Lesson 1 into your app. In fact, while the API provides the ability to configure your app programmatically (adding layers, symbolizing them, configuring popups, etc.), even the best coder is likely to find it helpful to use the ArcGIS Online GUI to reduce their coding burden.
5.1.1 Load a Web Map Into a 2D App
5.1.1 Load a Web Map Into a 2D App ksc17In Lesson 1, we saw that it's possible to use the GUI-based tools in ArcGIS Online to author web maps that can then be easily incorporated into apps created using a configurable app template or Experience Builder. It's also quite easy to do the same for apps developed using the Maps SDK for JS. Here's how:
- Copy one of your apps from Lesson 4 and name it jen_and_barry_2d.
- In the SDK documentation, browse to Sample Code > Mapping and Views > MapView (2D) > Load a basic web map > Explore in the sandbox.
This sample works by creating an object of the WebMap class and setting its portalItem property to a JS object defined simply as an id of a particular value. That WebMap is then used to set the map property of the MapView (in place of the plain Map object we saw in the previous lesson). Note the reference to “esri/WebMap” in the require() declaration.
Adapting this sample to your own liking is as simple as setting the portalItem’s id property to that of your own web map. - Copy the sample’s JS code into your own jen_and_barry_2d main.js file replacing your content with the sample's.
- In ArcGIS Online, browse to your web map from Lesson 1 and open it in the map viewer.
- In your browser’s address bar, you should see a URL ending in “webmap=” followed by a long alpha-numeric string. Copy that string to your clipboard. (Double-clicking somewhere on the string should select it.)
- Paste the string into your main.js file, replacing the portalItem id value from the sample.
- Test your app by opening it in a browser.
Two important points to keep in mind about consuming web maps:
- The map can be stored in ArcGIS Online as we saw here, or in your own organization’s ArcGIS Portal instance.
- You’ll want to make sure that the map has been shared with your intended audience; otherwise, it will not appear.
5.1.2 Load a Web Map Into a 3D App
5.1.2 Load a Web Map Into a 3D App ksc17We saw in the previous lesson that a Map can be associated with a MapView to produce a 2D app or with a SceneView to produce a 3D app. Likewise, a WebMap can be used as the basis for a 2D or 3D app.
Create a copy of your jen_and_barry_2d app and modify it so that your web map is displayed as a 3D app.
5.1.3 Load a Web Scene Into a 3D App
5.1.3 Load a Web Scene Into a 3D App ksc17Thus far in the course, we’ve seen how to drape 2D data over a 3D globe, but we haven’t really taken advantage of the 3D environment yet. Web scenes are basically the 3D analog to web maps, in which the scene features have a Z component.
Web scenes can be either global, for situations where the earth’s spherical nature comes into play, or local, where it does not. Global scenes are displayed in the Web Mercator coordinate system, whereas local scenes can be displayed in a local, planar coordinate system.
This documentation page on globes and local scenes shows earthquakes in an example of a global scene. And this visualization of building floors provides an example of a local scene.
As with web maps, web scenes are published and discoverable through ArcGIS Online or through local implementations of ArcGIS Portal. This search will provide a list of scenes owned by Esri’s 3D team, including models of many world cities.
Have a look at the Load a basic web scene sample in the documentation.
Note that the only difference from the jen_and_barry_3d app you created earlier is in the use of the WebScene class in place of the WebMap class.
5.2 Autocasting
5.2 Autocasting jed124In looking at the Load a basic web scene sample (or perhaps earlier samples), you may have noticed a comment “autocasts as new PortalItem().” So what does that mean? Certain properties in the SDK for JS have been coded behind the scenes by Esri so that our jobs are made a bit easier. Like all properties, the ones that support autocasting are written expecting to be set to a particular data type (e.g., string, number, object). Where these properties differ is that they can also be set to a JavaScript object that “looks like” the required object type. This is a concept that’s much easier to explain by example, so let’s work through what the Load a basic web scene code would look like if autocasting was not built into the SDK.
- Open the Core API Reference in the documentation and look up the WebScene class.
- Find the portalItem property and note that its data type is listed as PortalItem. Ordinarily, this would mean that you’d need to create an object of that class (using new PortalItem) to set the WebScene’s portalItem property. And of course, that would mean adding the PortalItem class to your require() declaration. Here is how the code would need to look if autocasting were not available, alongside how the sample is actually coded:
No autocasting used
const [SceneView, WebScene, PortalItem] = await $arcgis.import([
"@arcgis/core/views/SceneView.js",
"@arcgis/core/WebScene.js",
"@arcgis/core/portal/PortalItem.js"
]);
const myPortalItem = new PortalItem({
id: "3a9976baef9240ab8645ee25c7e9c096"
});
const scene = new WebScene({
portalItem: myPortalItem
});
const view = new SceneView({
map: scene,
container: "viewDiv",
padding: { top: 40 }
});Autocasting used
const [SceneView, WebScene] = await $arcgis.import([
"@arcgis/core/views/SceneView.js",
"@arcgis/core/WebScene.js"
]);
const scene = new WebScene({
portalItem: {
id: "3a9976baef9240ab8645ee25c7e9c096"
}
});
const view = new SceneView({
map: scene,
container: "viewDiv",
padding: { top: 40 }
});
So autocasting makes it a bit easier to set certain properties. The way to tell which properties behave this way is to look for the word autocast next to the property’s data type in the documentation.
Here’s a second example to drive the point home. In the previous lesson, we worked with the MapView class in creating simple 2D apps. In the samples we dealt with, the MapView center property was set using a syntax like this:
center: [-112, 38]
However, the documentation of the center property tells us that its data type is Point. Thus, if the property didn’t support autocasting, we’d need to add the Point class to the require() declaration and create a Point object using the class’s constructor. Here are three alternative versions of the Get started with MapView sample:
No Autocasting used
const [Map, MapView, Point] = await $arcgis.import([
"@arcgis/core/Map.js",
"@arcgis/core/views/MapView.js",
"@arcgis/core/geometry/Point.js"
]);
const map = new Map({
basemap: "streets"
});
const pt = new Point({
latitude: 65,
longitude: 15
});
const view = new MapView({
container: "viewDiv",
map: map,
zoom: 4,
center: pt
});Autocasting used
const [Map, MapView] = await $arcgis.import([
"@arcgis/core/Map.js",
"@arcgis/core/views/MapView.js"
]);
const map = new Map({
basemap: "streets"
});
const view = new MapView({
container: "viewDiv",
map: map,
zoom: 4,
center: {
latitude: 65,
longitude: 15
}
});Autocasting used (with lon/lat array)
const [Map, MapView] = await $arcgis.import([
"@arcgis/core/Map.js",
"@arcgis/core/views/MapView.js"
]);
const map = new Map({
basemap: "streets"
});
const view = new MapView({
container: "viewDiv",
map: map,
zoom: 4,
center: [15, 65]
});
The first version is the syntax we’d need if autocasting were not available on the center property. The second uses autocasting, as we saw in the PortalItem example above. The third is how the sample is actually written in the documentation. Unlike the PortalItem example, the property is set to a JavaScript array rather than a JavaScript object. This appears to be a special case supported by the MapView center property. In general, you should use an object, not an array, when working with an autocast property.
5.3 REST API
5.3 REST API jed124Thus far in the course, the apps we’ve built have consumed map services hosted by Esri on their ArcGIS Online platform. Another common source for map services is ArcGIS Server. Many organizations, in both the private and public sectors, implement their own instances of ArcGIS Server as a means of publishing their geographic data. The JS apps built by these organizations typically consume their own services.
Esri makes it possible to consume their web services through the two primary architectural styles employed by web developers today: SOAP (Simple Object Access Protocol) and REST (REpresentational State Transfer). The SOAP protocol was developed by Microsoft in the 1990s and can be thought of as object-oriented programming in which the objects live on some web server and the programming code that manipulates them lives on some client machine. REST came later and has overtaken SOAP in popularity because of its ease of use. RESTful web services, as they are sometimes called, progressively expose their functionality through URLs that can be invoked almost like properties and methods. They send responses to the client applications in the form of XML or JSON.
One important aspect of using this framework is that information on the various REST web services published through an ArcGIS Server instance can be discovered through a series of pages called the Services Directory. This directory can be viewed in a web browser using a URL of the form <server name>/arcgis/rest/services.
Developers working with services published through ArcGIS Server do so using Esri’s REST API. Esri had their own short (3-hour) web course that did a nice job of explaining how to use the REST API. They retired that course, but I did manage to capture the written lesson content from it -- Esri Training: Introduction to the ArcGIS for Server REST API (as PDF). Please work through this document, paying particular attention to the parts of the course dealing with layer services, as that is what we’ll be focusing on in this lesson.
You should also be sure to consult the documentation -- ArcGIS Resources: REST API.
5.4 Layer Types
5.4 Layer Types jed124ArcGIS Server publishes data layers of several different types, depending on whether the underlying data source is vector or raster, the amount of data being served, the needs of the apps consuming them, etc. In this part of the lesson, we’re going to walk through the most commonly used types, describing how they differ from one another, how they can be distinguished from layers of other types, under what circumstances they’re intended to be used, and how they are incorporated into an app.
In a moment, we'll take a detailed look at some commonly used layer types one by one. Before doing so, here are some important properties that are defined under the abstract Layer class that is the parent to all of the layer sub-classes:
- opacity: set to a value from 0 to 1, with 0 making the layer completely transparent and 1 making it completely opaque (solid),
- title: set to a string specifying how the layer is labeled in widgets like the Legend and LayerList,
- visible: set to a Boolean controlling whether or not the layer is turned on/off.
5.4.1 TileLayer
5.4.1 TileLayer ksc17Description:
- displays a full dataset using a series of adjacent tiles (smaller individual images that you can think of as snapshots of the complete map)
- a different set of tiles is created for each level of detail (zoom level)
- referred to as a cached service because the tiles are pre-created (i.e., not dynamically at the time the request for the layer is received by the server)
- can show data from many different underlying datasets at once
- often used as a basemap; in fact, most of the options when setting a Map’s basemap property result in the addition of a TileLayer
- changes in the underlying data cause the service to be out of date, so this layer type is best suited to data that changes infrequently
- tile creation can be a time consuming process; often automated to run overnight
- because you’re dealing with a picture of your data, you can’t implement popup windows
- can create your own using ArcMap or ArcGIS Pro (see GEOG 865)
How do I know I’m dealing with a TileLayer?
- labeled as a MapServer service in the REST Services Directory
- the service’s Single Fused Map Cache property will be true and it will have a Tile Info section
Class description in the SDK:
Example service:
Code sample:
- ArcGIS Maps SDK for JavaScript: Get started with layers
- TileLayer objects are created and stored in the transportationLyr and housingLyr variables
5.4.2 VectorTileLayer
5.4.2 VectorTileLayer ksc17Description:
- newer technology than raster tiles
- data are still cached, but aren’t just static pictures
- service passes geometry and styling info to client, which then does the rendering
- Attributes are not included, popups and other feature interactions are not available
- same set of tiles can be styled in many ways
- also mostly associated with basemaps; the -vector options of the Map’s basemap property result in the addition of a VectorTileLayer
- tile creation much shorter process than for raster tiles
- tile creation can be done in ArcGIS Pro, but not ArcMap
How do I know I’m dealing with a VectorTileLayer?
- unlike the other layer types described here, vector tile layers can only be discovered through ArcGIS Online or ArcGIS Portal
- in ArcGIS Online, when viewing the layer’s Details page, its Source will be listed as a Vector Tile Service and it will have a View Style button
Class description in the SDK:
Example service:
- several world-scale vector tile layers (all based on the same data, just styled differently) created by Esri and hosted on ArcGIS Online: ArcGIS Vector Basemaps
Code sample:
- ArcGIS Maps SDK for JavaScript Sandbox
- note the layer’s url property can be set to the service itself (and drawn with default styling) or to a JSON style object; see ArcGIS Maps SDK for JavaScript: VectorTileLayer
5.4.3 FeatureLayer
5.4.3 FeatureLayer ksc17Description:
- feature geometry and attributes sent to client; rendered on client
- features to stream to the client are often filtered using a definition expression
- having the geometry and attributes on the client enables the implementation of popup windows
- also supports several types of renderings (e.g., unique values, class breaks, etc.)
How do I know I’m dealing with a FeatureLayer?
- on ArcGIS Server instances, can be created from a service labeled as either MapServer or FeatureServer
- when dealing with a service containing multiple layers, must create the FeatureLayer from one of the service sub-layers
Class description in the SDK:
Example service:
Code sample:
5.4.4 MapImageLayer
5.4.4 MapImageLayer ksc17Description
- picture of your layer taken on the fly by the server and sent to client
- often a mix of different layers, each of which can be customized (renderer, definition expression, etc.)
- use if a FeatureLayer would be too much for client to handle
How do I create a MapImageLayer?
- On ArcGIS Server, instances can be created from a service labeled as MapServer
- can create from either the full service or a sub-layer
Class description in the SDK:
Example service:
Code sample:
5.4.5 ImageryLayer
5.4.5 ImageryLayer ksc17Description:
- stream of raster data from the server to the client device
- raster can have pixel filtering and rendering performed on the server or client
Class description in the SDK:
Example service:
Code sample:
5.5 Coding Tips
5.5 Coding Tips jed124We'll end this lesson with some content that will hopefully make you a bit more successful as a coder. We’ll talk about development environments (where you write your code) and debugging tools. But first, here are a few tips that apply regardless of the kind of programming you're doing:
- Write your code in baby steps. Start with something very simple (or a working sample that you modify slightly), test it, add a small piece, test again, etc. Avoid writing long blocks of code without testing. The more untested code you write, the harder it will be to identify errors.
- When inserting an opening brace, bracket or parenthesis, insert the matching closing character at the same time. If you wait to do that until after you've finished entering the intervening statements, you're apt to forget.
- When the JavaScript console reports multiple errors, fix just the first error, then reload the page. One error can cause several others to appear downstream; fixing it can cause the rest to disappear.
- As with writing in general, the best way to get past a block is often to take a break and come back to the code later. With a fresh look at your code, it may take just minutes to find a bug that you had banged your head against the wall about for hours the night before.
5.5.1 Development Environments
5.5.1 Development Environments ksc17- Download and install Notepad++.
- Open your hello_map app’s index.html page in Notepad++. An easy way to do this is to browse to it through your file system, right-click on it, and select Edit with Notepad++.
The first thing you should notice is the formatting applied to your code. HTML elements are shown in blue, attributes in red, and attribute values in purple. This happens because your file has a .html extension. Keep in mind that if you create a new page from scratch, you will not see this highlighting done until you save it with a .htm or .html extension.
Next, note the boxes just to the left of a few lines in your code. These boxes appear at the beginning of blocks of code that can be logically treated as a unit. - Click on the box next to the head start tag to collapse the code associated with the head element.

The minus that had been inside the box will change to a plus, indicating that clicking it again will re-expand that code unit. This is a feature of IDEs referred to as code folding.
- Move your cursor down through the document and note that when the cursor is located within a start tag, that tag and its matching end tag will be highlighted (and vice versa). This highlighting can be very helpful, especially if you haven't done a good job of indenting your code logically.
- Now open the app’s main.js file in Notepad++ and move the cursor down the document until you reach the line where you create the Map object. Move the cursor to the right until you reach the opening brace. You should see that the brace is highlighted and a red line appears connecting the opening brace to its matching closing brace. The same behavior can be observed with brackets and parentheses. Given how often these characters are used in JavaScript -- and the importance of ordering them correctly -- this feature can be quite useful.
5.5.2 Debugging
5.5.2 Debugging ksc17So, what do you do when you load your page in a browser and get...nothing? Thankfully, there are tools that can help pinpoint problems in your code without having to manually search line by line. Let's have a look at a couple of broken pages and how we can determine why they're broken.
- Load this Hello Map example in Chrome, Firefox, or Edge. Your browser window should be blank because of an error in the page.
- Open the JavaScript console as follows:
Chrome - click on the menu icon (3 stacked dots to the right of the address bar), then select More Tools > Developer tools > Console. (Or use the Ctrl-Shift-i shortcut.)
Firefox - click on the menu icon (3 stacked lines, also in the upper right of the window) and select Web Developer > Web Console.
MS Edge - click on the menu icon (3 stacked dots, also in the upper right of the window), then select More Tools > Developer tools > Console.
You should see an error message like the following, depending on your browser. (The console may report warnings in addition to errors, in which case you’ll need to skip over the warnings or toggle them off.)
Uncaught SyntaxError: Unexpected identifier 'center'
The browser ran into a problem executing the JS code beginning with the word center in main.js. It should tell you the line number and the character number where the error occurred as well. (Many IDEs report the location of the cursor when editing a document; in Notepad++ you'll find this info in the bottom middle of the application window.)
- If this was your own page, you could have a look at that line in Notepad++ or whatever editor you are using. Let's just look at the source code in the browser in this case.
- Within the Console, click on the link listed next to the error message to view the code being loaded from that file. Depending on the browser you’re using, the code lines may be numbered. The "problem" line may also be highlighted or have the cursor on it. (If the .js code won't load in the Console, try reloading the page with the Developer Tools pane open.)
As is sometimes the case, the problem here can be found just before the flagged line: there is no comma after the setting of the zoom property on the previous line. It may seem confusing that the browser is flagging one line when the line requiring a fix is the previous one, but it actually makes sense if you remember that a property-value setting doesn’t need to be followed by a comma (i.e., when it’s the last one associated with the object). So the browser flags the line where it’s finding something unexpected. (It expects a comma or closing brace after the zoom property.) - Load the second Hello Map example in Chrome, Firefox, or Edge. Again, your browser window should be blank because of an error in the page.
- Using the error checking steps outlined above, you should identify the following error:
Uncaught SyntaxError: Unexpected token ')' on line 15
Line 15 is the last line of the script file -- a closing parenthesis, followed by a closing brace, followed by a semi-colon. If you were to look at the code in Notepad++ and move the cursor around those characters, the IDE would highlight for you the opening characters that match up to the closing characters. Hopefully you'll see the issue here is that the opening parenthesis came before the opening brace, but the closing characters are in the wrong order. The closing brace needs to come before the closing parenthesis.
While checking the console for errors and using IDE syntax highlighting can often be a big help, there are likely to be times when you're unable to identify the problem in your code. Another method for pinpointing problems is to produce some output at strategic points in your code. This could be as simple as an alert() statement that confirms that code execution reached a certain point, like
alert("Finished adding overlays");
You might also use an alert() box to display the contents of a variable or the property of some object:
alert("The coords are: " + pt.latitude() + ", " + pt.longitude());
An alternative to using alert() in this way is to write messages to the JavaScript console. This is especially preferable when you want to diagnose a problem with a loop and would rather not have to dismiss alert() boxes repeatedly. Writing messages to the console can be done as follows:
console.log("The value of i is " + i);
Over years of web development, I have found that these simple debugging strategies usually lead me to finding and fixing my errors.
5.5.3 JSLint/JSHint
5.5.3 JSLint/JSHint ksc17One strategy for avoiding errors when you test your apps in a browser is to catch those errors in your development environment. A linter is a tool that is used to analyze code for potential errors before execution. Linters exist for many programming languages, but in a JavaScript context, JSLint is perhaps the best known. You can use JSLint online by navigating to JSLint, pasting your JS code into the big text box and clicking the JSLint button.
JSLint was developed by Doug Crockford in 2002 and the standards his linter enforces are described on the JSLint website. Many have found some of the JSLint rules to be overly strict (e.g., statements within a function are required to be indented exactly 4 spaces). For this reason, a more flexible alternative was developed as a community-driven open-source project: JSHint.
Many IDEs come equipped with one or both of these tools. Others can have the tools added through a plugin. Notepad++ had a JSLint plugin, but it does not work with the 64-bit version of the IDE (which you're likely to have installed). CodePen provides an Analyze JavaScript option on the JS editor panel's dropdown menu that is based on JSHint.
Pay attention to the availability and implementation of JSLint/JSHint in the IDE review portion of this week's assignment.
5.5.4 Beautifiers
5.5.4 Beautifiers ksc17Especially when cobbling together snippets of code from various samples, it’s common for your code to become messy (e.g., indented inconsistently, varying amounts of whitespace, etc.). IDEs typically provide ways to indent or unindent blocks of code. In Notepad++, this can be done by highlighting the incorrectly indented code and hitting Tab or Shift-Tab, respectively. However, there are also beautifying tools available both online and within IDEs that can correct multiple kinds of errors across an entire document in seconds.
Here is a messy version of the Hello_Map JS code:
const [Map, MapView] = await $arcgis.import([
"@arcgis/core/Map.js",
"@arcgis/core/views/MapView.js",
]);
const map = new Map({
basemap: "streets"
});
const view = new MapView({
container: "viewDiv",
map: map,
zoom: 4,
center: [15, 65]
});
This code, while perfectly valid as seen by browsers, is difficult for people to read because of the inconsistent indentation and large block of whitespace.
- Point your browser to jsbeautifier.org.
- Copy the messy code above and paste it into the large textbox on the jsbeautifier site.
- Click the Beautify JavaScript or HTML button either above or below the text box (or hit Ctrl-Enter). Voilà! The messiness of this code is cleaned up.
There are several settings in the upper right of the page that you can use to tweak how the beautifier tidies your code. The first four dropdown lists are probably of the most interest:
- The first allows you to specify the number of spaces you wish to indent with (or use 1 tab instead). It’s considered good practice to use either tabs or spaces, but not to mix the two in the same document. Developers often have strong opinions in the space vs. tab debate. I lean toward using spaces since a space occupies one character regardless of the app you’re using to view the code, whereas a tab could be shown as 2, 4 or 8 characters wide depending on the app.
- The second list has to do with the amount of whitespace you want to appear between blocks of code (e.g., between two functions, after a loop or conditional expression, or as in this case, between the definition of two object variables).
- The third list allows you to specify the maximum width of your code before it gets wrapped to the next line.
- The fourth list involves the positioning of braces. Some developers (myself included) prefer to position the opening brace that goes with a function or object constructor on the same line as its “control statement”. Others prefer that the brace be placed on its own line.
I encourage you to experiment with these options to see how they affect the formatting of your code.
As with the linting tools, IDEs sometimes include a built-in beautifying tool or can have one added as a plugin. The only tool that I’m aware of for Notepad++ is JSToolNpp. I’m not going to walk you through the use of this tool since I’m not crazy about how it formats object definitions. For example, it formats an object like this:
const map = new Map({
basemap: "streets"
});
whereas I’d prefer it to be formatted like this:
const map = new Map({
basemap: "streets"
});
However, you are welcome to install and play with the tool if you like.
Finally, you should note that CodePen offers a beautifier on the JS editor panel's dropdown menu (Format JavaScript).
5.5.5 Style Guides
5.5.5 Style Guides ksc17In the previous sections, we saw that both linting and beautifying tools can be customized based on personal preference. Application development often involves multiple coders contributing pieces to the end product. Editing application code, whether fixing bugs or adding new functionality, is much easier when all of the code is formatted in the same way. When code jumps between different formats (i.e., style rules), anyone trying to interpret the code is forced to waste valuable time adjusting his/her mindset to the different formats. And even in organizations in which team coding is not employed, it is rare that an application will only ever be modified by the original author. Coders go on vacation, leave the company, etc. For these reasons, many organizations go to the trouble of constructing a style guide – a set of rules that all of their coders will follow. Here is a JavaScript style guide used at Google:
Many of these rules are fairly advanced, but there should be at least a few you can grasp. For example, the guide's authors specify that all statements must be ended in a semi-colon even though you might be able to get by with omitting them. Multiline string literals should be constructed using string concatenation rather than the line continuation character. Under the Code formatting heading, you’ll find some of the personal preference items we discussed in the beautifying section. Opening braces should be found on the same line as what they’re opening, property settings in an object definition should be indented two spaces, etc.
If you work in an organization that already does JS development and you’re going to be joining a development team, you should look into whether there is a style guide you should be following. If you’re just starting a new team, it might be a good idea to put some thought into creating a guide.
Assignment
Assignment jed124The coding assignment for this lesson is in three parts. Here are some instructions for completing the assignment.
Although we learned about adding WebMaps and WebScenes to our apps, for full credit on Parts I and II you will need to add layers to the Map as discussed in 5.4 Layer Types. This will be useful in the next lessons where we need to access layers for custom symbology, queries, and features.
Part I
The Transportation Planning and Programming (TPP) division of the Texas Department of Transportation has published vector tile basemaps for Texas as services through ArcGIS Online. Find one of these services and create an app that displays its data in a 3D scene, zoomed in to the City of Houston.
Hint: You can narrow down your search in ArcGIS Online to different content types like Maps, Layers, Scenes, etc. Some content types are broken into sub-categories as well. Use the ability to search by content type along with appropriate search terms to identify the correct service. (Make sure you're not searching only the Penn State group content!)
If you're unable to find the described service, create a 3D app that displays data from some other vector tile service for partial credit.
Part II
One of Esri's sample ArcGIS Server instances hosts a map service that contains U.S. cities, interstates, and state/county boundaries:
Build an app that displays just the county boundaries on a 2D map. In your solution, you should be taking a snapshot of the data on the server and passing that to the client rather than passing the features themselves. Be sure to read the documentation carefully for how to display just the county sublayer. It might be a good idea to display all of the sublayers first before attempting to filter out some of them.
Part III
In this lesson, you began using a relatively basic IDE, Notepad++. For the last part of this week's assignment, I'd like you to download and experiment with a different IDE (Visual Studio Code, Sublime, WebStorm, Eclipse, Netbeans, Komodo), then share your thoughts on it in a recorded video. If you know of a different javascript IDE that you'd like to review, let me know! Here are some detailed instructions:
- Please go to the Assignment 5 IDE Review Sign-up page in the Lesson 5 module in Canvas to sign up for an IDE. The sign-ups will be set up such that the IDEs receive more or less equal coverage. (If there's another IDE that you'd like to evaluate, check with the instructor first.)
- Limit your video to 5 minutes.
- In your demo, be sure to discuss the IDE features that were mentioned in the lesson. And feel free to talk about any interesting features that were not in the lesson. (If demonstrating the IDE, please use files from a previous assignment rather than this week's.)
- Give your thoughts on whether you plan to use the IDE instead of Notepad++.
Deliverables
This project is one week in length. Please refer to the Canvas course Calendar for the due date.
NOTE: You'll need to zip the files to upload to the Canvas dropbox.
- Upload the HTML, CSS, and JavaScript files associated with your TX DOT vector tile app. Don't post this app to your e-portfolio. (40 of 100 points)
- Upload the HTML, CSS, and JavaScript files associated with your county boundary app. Don't post this app to your e-portfolio. (40 of 100 points)
- Post a link to your IDE evaluation video to the Lesson 5 Discussion Forum. (20 of 100 points)
- Complete the Lesson 5 quiz.
Summary and Final Tasks
Summary and Final Tasks jed124In this lesson, you saw how to incorporate a web map you created using ArcGIS Online tools into a JavaScript API app and how Esri's REST API can be used to interact with services published through ArcGIS Server. You also learned about the various layer types built into the API and the circumstances under which each type might be used. Finally, you learned about some more generic web development topics, including Integrated Development Environments (IDEs) and debugging strategies.
Knowing the different layer types found in Esri's API and how to add them to a map is an important first step. However, chances are you'll want to customize the symbology of your map's layers so that they convey the desired information more effectively. That will be the focus of Lesson 6.