ArcGIS API for JavaScript: Getting Started
ArcGIS API for JavaScript Sample Code
These have not been updated in three or four years, but they used to be awesome. Check out the hackerlabs
If you continue with the Advanced course next semester, you will learn JavaScript. We will use CodePen.io as a tool to write and test code.
Jacob Wasilkowski
What's the purpose of each and how do they work together?
READ 👏 THE 👏 DOCS 👏
https://developer.mozilla.org/en-US/docs/Web
// variable declaration
let myString;
// and then later assignment
myString = 'sample string';
// variable declaration and assignment at same time
let myNumber = 90210;
console.log(myNumber - 90209); // simple math
// strings
let question = 'Is pizza awesome?';
let answer = 'Without a doubt.';
let qAndA = question + ' ' + answer; // concatenate strings
console.log(qAndA);
// format with template strings
console.log(`${question} ${answer}`);
// booleans
console.log(1.5 === 1.5); // true
console.log(1 + 1 === 3); // false
// arrays
let myArray = [1, 2, 3, 'x', 'y', 'z'];
console.log(myArray[3]);
myArray.push(100);
let stringFromArray = myArray.join(' and ');
console.log(stringFromArray);
// functions
let sumThreeNumbers = function(a, b, c) {
// do something with the function arguments
console.log(a, b, c);
// return the sum
return a + b + c;
};
// objects
let myObject = {
name: 'Greg',
totalCats: 15,
faveLanguages: ['Python', 'JavaScript'],
isInCharge: true,
tellMeSomethingInteresting: function() {
return this.name + ' has ' + this.totalCats + ' cats.'
}
};
console.log(myObject.name);
console.log(myObject.tellMeSomethingInteresting());
// conditional statements
let whatHappened;
if (1 === 2) {
whatHappened = 'bad math';
} else if (true && false) {
whatHappened = 'bad logic';
} else {
whatHappened = 'we got to the "else!"';
}
console.log(whatHappened);
Start thinking in terms of user interaction "events" instead of top-to-bottom script execution.
Obligatory JS meme
Exercise: create index.html
page, insert <script>
tag, and try out the Chrome developer tools.
<!DOCTYPE html>
<html>
<head>
<title>Title for browser tab.</title>
<style>
/* this is a CSS comment */
.green {
color: green;
/* color: rgb(0, 128, 0); */
/* color: #008000; */
}
.italic-sans-serif {
font-style: italic;
font-family: sans-serif;
}
</style>
</head>
<body>
<!-- this is an HTML comment -->
<h1>
Hello, World! (I'm an "h1" element).
</h1>
<h2>
Subtitle (I'm an "h2" element).
</h2>
<p>
Hello, World! (I'm a "p" element.)
</p>
<p class="green">
Hello, World! (I'm a "p" element with a CSS class.)
</p>
<div>
This is a "div" element.
</div>
<div class="green italic-sans-serif">
This is a "div" with several CSS classes.
</div>
<a href="https://www.slu.edu/arts-and-sciences/earth-atmospheric-sciences/" target="_blank">
This is a link that will open in another window.
</a>
<p>
Next are a "button" element followed by a "p" element, both of which are inside of a "div" element.
</p>
<div>
<button id="coolButton">
Increase the "clickCount" JavaScript variable.
</button>
<p id="clickCountDisplay"></p>
</div>
<script>
// this is a JavaScript comment
let coolButtonReference = document.querySelector('#coolButton');
let clickCountDisplayReference = document.querySelector('#clickCountDisplay');
let clickCount = 0;
clickCountDisplayReference.innerText = clickCount;
coolButton.addEventListener('click', function () {
console.log('Hey, I got clicked!');
clickCount = clickCount + 1;
// another way to write the same command would be:
// clickCount += 1;
console.log('clickCount: ', clickCount);
clickCountDisplayReference.innerText = clickCount;
});
</script>
</body>
</html>
-
Intros to ArcGIS API for JavaScript and LeafletJS
-
Getting started with CodePen
- Why? Your browser will block functionality if browsing files directly from hard drive. You must use a web server. CodePen is interactive and easy to experiment with, just like Python Notebooks.
-
Leaflet exercise
-
Copy and paste the contents of
leaflet-demo.html
into a new CodePen. -
Set the map's initial position to be centered at and zoomed to St. Louis.
-
Change the basemap to a different Esri basemap.
-
Add a "marker" positioned at SLU onto the map.
-
-
Esri exercise
-
Copy and paste the contents of
esri-demo.html
into a new CodePen. -
Make the Esri demo into 3D! You can instruct the code to use a
SceneView
instead of aMapView
.
-
-
LeafletJS (and optionally the Esri-Leaflet plugin)
-
"JavaScript: The Good Parts", Douglas Crockford (2008)
- A must-read which will go by quickly! It is available at SLU Libraries...you don't really have a good excuse to avoid this book.
-
JavaScript for Cats 🐈 🐱 🐈
-
Mozilla Dev Network: A re-introduction to JavaScript (JS tutorial)
This is optional, but the students who complete it tell me that they got a lot out of it. If you want to learn more about Javascript and the ArcGIS JSAPI, complete the following two lessons:
Copy and past the code into text files and submit the text files.