Building Web and Mobile ArcGIS Server Applications with JavaScript - Second Edition - Eric Pimpler - E-Book

Building Web and Mobile ArcGIS Server Applications with JavaScript - Second Edition E-Book

Eric Pimpler

0,0
45,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Master the ArcGIS API for JavaScript to build web and mobile applications using this practical guide.

About This Book

  • Develop ArcGIS Server applications with JavaScript, both for traditional web browsers as well as the mobile platform
  • Make your maps informative with intuitive geographic layers, user interface widgets, and more
  • Integrate ArcGIS content into your custom applications and perform analytics with the ArcGIS Online

Who This Book Is For

If you are a web or mobile application developer, who wants to create GIS applications in your respective platform, this book is ideal for you. You will need Java Script programming experience to get the most out of this book. Although designed as an introductory to intermediate level book, it will also be useful for more advanced developers who are new to the topic of developing applications with ArcGIS Server.

What You Will Learn

  • To create an application with the ArcGIS API for JavaScript
  • Build and display a broad range of different geometry types to represent features on the map
  • The best way to leverage a feature layer and display related attribute data
  • The functionality of the wide range of widgets and how to use them effectively
  • Query data to gain new insights into the information it contains
  • Work with tasks to discover and locate features on the map
  • Using the geocoder and associated widgets
  • The ability of the API to provide turn by turn directions and routing capabilities
  • How to use the Geometry Engine and Geometry Service tasks for common geoprocessing operations
  • Integrate content on ArcGIS online and add it to your custom web mapping application

In Detail

The ArcGIS API for JavaScript enables you to quickly build web and mobile mapping applications that include sophisticated GIS capabilities, yet are easy and intuitive for the user.

Aimed at both new and experienced web developers, this practical guide gives you everything you need to get started with the API. After a brief introduction to HTML/CSS/JavaScript, you'll embed maps in a web page, add the tiled, dynamic, and streaming data layers that your users will interact with, and mark up the map with graphics. You will learn how to quickly incorporate a broad range of useful user interface elements and GIS functionality to your application with minimal effort using prebuilt widgets. As the book progresses, you will discover and use the task framework to query layers with spatial and attribute criteria, search for and identify features on the map, geocode addresses, perform network analysis and routing, and add custom geoprocessing operations. Along the way, we cover exciting new features such as the client-side geometry engine, learn how to integrate content from ArcGIS.com, and use your new skills to build mobile web mapping applications.

We conclude with a look at version 4 of the ArcGIS API for JavaScript (which is being developed in parallel with version 3.x) and what it means for you as a developer.

Style and approach

Readers will be taken through a series of exercises that will demonstrate how to efficiently build ArcGIS Server applications for the mobile and web.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 301

Veröffentlichungsjahr: 2017

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Building Web and Mobile ArcGIS Server Applications with JavaScript

Second Edition

 

 

 

 

 

 

Build exciting custom web and mobile GIS applications with the ArcGIS Server API for JavaScript

 

 

 

 

 

 

Eric Pimpler
Mark Lewin

 

 

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Building Web and Mobile ArcGIS Server Applications with JavaScript

Second Edition

 

Copyright © 2017 Packt Publishing

 

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

 

First published: February 2014

Second edition: October 2017

Production reference: 1201017

 

Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.

ISBN 978-1-78728-052-6

www.packtpub.com

Credits

Authors

Eric Pimpler

Mark Lewin

Copy Editor

Safis Editing

Reviewers

Ken Doman

Adeyemo Ayodele

Project Coordinator

Vaidehi Sawant

Commissioning Editor

Aaron Lazar

Proofreader

Safis Editing

Acquisition Editor

Chaitanya Nair

Indexer

Aishwarya Gangawane

Content Development Editor

Zeeyan Pinheiro

Graphics

Jason Monterio

Technical Editor

Ketan Kamble

Production Coordinator

Shantanu Zagade

 

About the Authors

Eric Pimpler is the founder and owner of GeoSpatial Training Services (geospatialtraining.com) and has over 20 years of experience in implementing and teaching GIS solutions using Esri, Google Earth/Maps, and open source technology. Currently, he focuses on ArcGIS scripting with Python and the development of custom ArcGIS Server web and mobile applications using JavaScript. He is the author of Programming ArcGIS 10.1 with Python Cookbook. Eric has a bachelor's degree in geography from Texas A&M University and a master's degree in applied geography with a specification in GIS from Texas State University.

Mark Lewin has been developing, teaching, and writing about software for over 16 years. His main interest is GIS and web mapping. Working for ESRI, the world's largest GIS company, he has been a consultant, trainer and course author, and a frequent speaker at industry events. He has subsequently expanded his knowledge to include a wide variety of open source mapping technologies and a handful of relevant JavaScript frameworks, including Node.js, Dojo, and JQuery. Mark now works within Oracle's MySQL curriculum team, focusing on creating great learning experiences for DBAs and database developers, but remains crazy about web mapping.

About the Reviewers

Ken Doman is a senior frontend engineer at GEO Jobe, a geographic information systems consultant company and ESRI business partner that helps public sector organizations and private sector businesses get the most out of geospatial solutions. Ken has worked for the municipal government and in the private sector. He has experienced many facets of GIS technology, from field data collection to mapping and data analysis to creating and deploying web mapping applications and solutions.

Ken is the author of Mastering ArcGIS Server Development with JavaScript. He has also reviewed several books for Packt Publishing, including Building Web and Mobile ArcGIS Server Applications with JavaScript and Spatial Analysis with ArcGIS by Eric Pimpler, ArcGIS for Desktop Cookbook by Daniela Christiana Docan, and ArcPy and ArcGIS by Silas Toms.

I’d first like to thank my wife, Luann, who puts up with my late nights reviewing books like this. I’d like to thank my current employer, GEO Jobe GIS Consulting, as well as past employers like Bruce Harris and Associates, City of Plantation, FL and City of Jacksonville, TX for believing in me and letting me learn so much on the job. Finally, I’d like to thank my creator for putting me where I need to be.

Ayodele Adeyemo is a geogeek with over four years experience, and is passionate about leveraging data and technology to provide solutions to human, social, and technological problems. He currently works with eHealth Africa in Nigeria with the Global Health and Informatics department to provide user data and technology to solve health issues across communities in Africa. He has experience in building and mentoring geospatial solutions as well as consulting for organizations on how to deploy sustainable geospatial solutions to support their agencies and projects.

Ayodele has worked as GIS Specialist at eHealth Africa andFounder and Latitudes Tech Limited. He has worked on the bookArcPy and ArcGIS: Automating ArcGIS for Desktop and ArcGIS Online with Python.

I'd like to thank my manager, Dami Sonoiki; my supervisor, Samuel Aiyeoribe; and my colleagues Samuel Okoroafor and Seun Egbinola.

www.PacktPub.com

For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www.packtpub.com/mapt

Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.

Why subscribe?

Fully searchable across every book published by Packt

Copy and paste, print, and bookmark content

On demand and accessible via a web browser

Customer Feedback

Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1787280527.

If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!

Table of Contents

Preface

What this book covers

What you need for this book

Who this book is for

Conventions

Reader feedback

Downloading the example code

Downloading the color images of this book

Errata

Piracy

Questions

Introduction to HTML, CSS, and JavaScript

Basic HTML page concepts

DOCTYPE

Primary tags

Validating HTML code

JavaScript fundamentals

Commenting code

Variables

JavaScript is case sensitive

Variable data types

Decision support statements

Looping statements

Functions

Objects

Basic CSS principles

CSS syntax

Inline styling

Internal style sheet

External style sheet

Separating HTML, CSS, and JavaScript

Summary

Creating Maps and Adding Layers

The ArcGIS API for JavaScript Sandbox

Basic steps for creating an application with the ArcGIS API for JavaScript

Creating HTML code for the page

Referencing the ArcGIS API for JavaScript

Loading API modules

Making sure the DOM is available

Creating the map

Creating the page content

Styling the page

The entire code

More about the map

Working with map service layers

Using the layer classes

Tiled map service layers

Dynamic map service layers

Adding layers to the map

Setting the visible layers from a map service

Setting a definition expression

Map navigation

Map navigation widgets and toolbars

Map Navigation with the mouse and keyboard

Getting and setting the map extent

Map events

Summary

Adding Graphics to the Map

The four parts of a Graphic

Specifying graphic geometry

Symbolizing graphics

Assigning attributes to graphics

Changing graphic attributes in an InfoTemplate

Creating the graphic

Adding graphics to the GraphicsLayer

Multiple GraphicsLayers

Practice time

Summary

The Feature Layer

Creating the FeatureLayer

Optional constructor parameters

Defining the display mode

The snapshot mode

The on-demand mode

The selection only mode

The auto mode

Setting a definition expression

Feature selection

Rendering FeatureLayer

Practice time

Summary

Using Widgets and Toolbars

Adding toolbars to an application

Steps for creating a toolbar

Defining CSS styles

Creating the buttons

Creating an instance of the Navigation toolbar

User interface widgets

The BasemapGallery widget

Basemap toggle widget

Bookmarks widget

The Print widget

Layer List widget

Time to practice

Search widget

Gauge widget

Measurement widget

The Popup widget

Legend widget

OverviewMap widget

Scalebar widget

Directions

HistogramTimeSlider

HomeButton

LocateButton

TimeSlider

LayerSwipe

The Analysis widgets

Feature editing

FeatureService

The Editing widgets

The Editor widget

TemplatePicker widget

AttributeInspector widget

The AttachmentEditor widget

The Edit toolbar

Summary

Performing Spatial and Attribute Queries

Introducing tasks in ArcGIS Server

Overview of attribute and spatial queries

The Query object

Setting query properties

Attribute queries

Spatial queries

Limiting the fields returned

Executing the query with QueryTask

Getting the results of the query

Practice time with spatial queries

Summary

Identifying and Finding Features

Using IdentifyTask to access feature attributes

Introducing IdentifyTask

IdentifyParameters

IdentifyTask

IdentifyResult

Practice time - implementing identify functionality

Using FindTask to access feature attributes

FindParameters

FindTask

FindResults

Summary

Turning Addresses into Points and Points into Addresses

Introduction to geocoding

Geocoding with a locator service in the ArcGIS API for JavaScript

Input parameter object

Input JSON address object

Input point object

Locator object

The AddressCandidate object

The geocoding process

The reverse geocoding process

Practice time with the locator service

The Search widget

Summary

Directions and Routing

Routing task

Practice time with routing

The Directions widget

ClosestFacility Task

ServiceArea task

Summary

Geoprocessing Tasks

Models in ArcGIS Server

Using the Geoprocessor task - what you need to know

Understanding the services page for a geoprocessing task

Input parameters

The Geoprocessor task

Executing the task

Synchronous tasks

Asynchronous tasks

Practice time with geoprocessing tasks

Summary

Geometry Operations

The Geometry Service

Geometry Service operations

Using the Geometry Service

The Geometry Engine

Practice time with the Geometry Engine

Summary

Integration with ArcGIS Online

Adding ArcGIS Online maps to your applications by using a webmap ID

Adding ArcGIS Online maps to your applications with JSON

Practice time with ArcGIS Online

Summary

Creating Mobile Applications

Compact build of the API

Setting the viewport scale

Practice time with the compact build

Integrating the geolocation API

Practice time with the geolocation API

Summary

Looking Ahead - Version 4 of the ArcGIS API for JavaScript

Steps for creating 2D maps

Accessing layers

New and changed layers

GraphicsLayer

FeatureLayer

MapImageLayer

VectorTileLayer

GroupLayers

SceneLayers

3D mapping and symbology

Scenes

Creating the map

Setting elevation data

Setting the camera

Specifying the environment

Local scenes

3D symbology and rendering

Summary

Preface

ArcGIS Server is the predominant platform for developing GIS applications for the web. In the past, you could choose from a number of programming languages to develop web mapping applications with ArcGIS Server, including JavaScript, Flex, and Silverlight. However, the Flex and Silverlight APIs have now retired, leaving JavaScript as the preferred language for developing applications on this platform. Its advantages over other languages include the fact that you can use it to build both web and mobile applications and that it does not require the installation of a plugin: everything runs natively in the browser.

This book will teach you how to build web-based GIS applications using the ArcGIS API for JavaScript. Using a practical hands-on style of learning, you will learn how to build fully functional applications with ArcGIS Server and, in doing so, develop a skillset that is in high demand.

You will learn how to create maps and add geographic layers from a variety of sources, including tiled and dynamic map services. In addition, you'll learn how to add vector graphics to the map and stream geographic features to the browser using a feature layer. Most applications also include specific functionality implemented by ArcGIS Server as tasks. You'll learn how to use the various tasks provided by ArcGIS Server to perform common GIS operations, including queries, identification of features, finding features by attribute, geoprocessing tasks, and more. Finally, you'll learn just how easy it is to develop mobile applications with the ArcGIS API for JavaScript.

What this book covers

Chapter 1, Introduction to HTML, CSS, and JavaScript, covers the fundamental HTML, CSS, and JavaScript concepts before getting started with developing GIS applications with the ArcGIS API for JavaScript.

Chapter 2, Creating Maps and Adding Layers, shows how to create a map and add layers to the map. You will learn how to create an instance of the Map class, add layers of data to the map, and display this information on a web page. Map is the most fundamental class in the API as it provides the canvas for your data layers and any subsequent activities that occur in your application. However, your map is useless until you add layers of data. There are several types of data layers that can be added to a map, including tiled, dynamic, feature, and others. You will learn more about each of these layer types in this chapter as well.

Chapter 3, Adding Graphics to the Map, demonstrates how to display temporary points, lines, and polygons in GraphicsLayer on the map. The GraphicsLayer is a separate layer that always resides on top of any other layers and stores all graphics associated with the map.

Chapter 4, The Feature Layer, describes FeatureLayer. This inherits from the GraphicsLayer, but offers additional capabilities such as the ability to perform queries and selections. Feature layers are also used for online editing of features. Feature layers differ from tiled and dynamic map service layers because feature layers bring geometry information to the client computer to be drawn and stored by the web browser. Feature layers potentially cut down on round trips to the server. A client can request the features it needs and perform selections and queries on those features without having to request more information from the server.

Chapter 5, Using Widgets and Toolbars, covers out-of-the-box widgets that you can drop into your application for enhanced productivity. These include the basemap gallery. Bookmark, print, and overview map user interface components. In addition, the ArcGIS API for JavaScript also includes helper classes for creating toolbars within your applications, such as navigation and drawing toolbars.

Chapter 6, Performing Spatial and Attribute Queries, covers the ArcGIS Server Query Task that allows you to perform attribute and spatial queries against data layers in a map service that have been exposed. You can also combine these query types to perform a combination attribute and spatial query.

Chapter 7, Identifying and Finding Features, covers two common operations found in any GIS application: clicking a feature on the map to identify it, or performing a query to locate features with specific attribute values. In either case, information about particular features is returned. In this chapter the reader will learn how to use the IdentifyTask and FindTask objects to obtain information about features.

Chapter 8, Turning Addresses into Points and Points into Addresses, covers the use of the Locator task to perform geocoding and reverse geocoding. Geocoding is the process of assigning a coordinate to an address while reverse geocoding assigns an address to a coordinate.

Chapter 9, Directions and Routing, describes how to access ArcGIS Server network analysis services to perform analyses on street networks, such as finding the best route from one address to another, finding the closest school, identifying a service area around a location, or responding to a set of orders with a fleet of service vehicles.

Chapter 10, Geoprocessing Tasks, allows you to execute custom geoprocessing workflows that you define in ArcGIS Pro desktop software using Model Builder or code as Python scripts. Once published to ArcGIS Server as geoprocessing services, these can be accessed from within your web mapping applications. This is a very powerful feature, and we'll get into one in this chapter.

Chapter 11, Geometry Operations, describes how to use the ArcGIS Server Geometry Service, and its client-side counterpart, the Geometry Engine, to execute common geometric operations such as buffering a feature and reprojecting from one coordinate system to another.

Chapter 12, Integration with ArcGIS Online, details how you can use the ArcGIS API for JavaScript to access data and maps created with ArcGIS.com, which is a web site for working with maps and other types of geographic information. On this site, you will find applications for building and sharing maps. You will also find useful basemaps, data, applications, and tools that you can view and use along with communities you can join. For application developers the really exciting news is that you can integrate ArcGIS.com content into your custom developed applications using the ArcGIS API for JavaScript. This chapter shows you how.

Chapter 13, Creating Mobile Applications, describes how to build mobile GIS applications using the ArcGIS API for JavaScript. ArcGIS Server support is currently provided for iOS and Android devices. In this chapter, you'll learn about the compact build of the API that makes web mapping applications possible through the use of Dojo Mobile.

Appendix, Looking Ahead at Version 4 of the ArcGIS API for JavaScript, gives you a broad overview of what to expect in version 4 of the API. This is a complete reworking of the API that is being developed in parallel with version 3.x.

What you need for this book

To complete the activities in this book, you will need access to a web browser--preferably Google Chrome or Firefox. Each chapter contains practices designed to supplement the material presented. You will complete these practices using the ArcGIS API for JavaScript Sandbox to write and test your code. The sandbox can be found at https://developers.arcgis.com/javascript/3/sandbox/sandbox.html. All the practices will access publicly available instances of ArcGIS Server, so you will not need to install ArcGIS Server yourself.

Who this book is for

This book is written for application developers who want to develop web and mobile GIS applications using ArcGIS Server and the API for JavaScript. It is primarily oriented toward beginning and intermediate-level GIS developers or to more traditional application developers who may not have developed GIS applications in the past but who are now tasked with implementing solutions on this platform. No prior experience with ArcGIS Server, JavaScript, HTML, or CSS is expected, but it is certainly helpful.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you. You can download the code files by following these steps:

Log in or register to our website using your e-mail address and password.

Hover the mouse pointer on the

SUPPORT

tab at the top.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box.

 

Select the book for which you're looking to download the code files.

Choose from the drop-down menu where you purchased this book from.

Click on

Code Download

.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for Windows

Zipeg / iZip / UnRarX for Mac

7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Building-Web-and-Mobile-ArcGIS-Server-Applications-with-JavaScript-Second-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/Building-Web-and-Mobile-ArcGIS-Server-Applications-with-JavaScript-Second-Edition_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at [email protected] with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.

Introduction to HTML, CSS, and JavaScript

There are certain fundamental concepts that you need to understand before you can get started with developing GIS applications with the ArcGIS API for JavaScript. For those of you already familiar with HTML, JavaScript, and CSS you may wish to skip ahead to the next chapter. However, if you're new to any of these concepts read on. We are only going to cover these topics at a very basic level. Just enough to get you started. For a more advanced treatment of any of these subjects there are many learning resources available including books and online tutorials. Please consult Appendix for a more comprehensive list of these resources.

In this chapter we will cover the following topics:

Basic HTML page concepts

JavaScript fundamentals

Basic CSS principles

Basic HTML page concepts

Before we dive into the details of creating a map and adding layers of information you need to understand the context of where the code will be placed when you're developing applications with the ArcGIS API for JavaScript. The code you write will be placed inside an HTML page or JavaScript file. HTML files typically have a file extension of .html or .htm and JavaScript files have an extension of .js. Once you have created a basic HTML page you can then go through the necessary steps to create a basic map with the ArcGIS API for JavaScript.

The core of a web page is an HTML file. Coding this basic file is quite important as it forms the basis for the rest of your application. Mistakes that you make in the basic HTML coding can result in problems further down the line when your JavaScript code attempts to access these tags.

The following is a code example for a very simple HTML page. This example is about as simple as an HTML page can get. It contains only the primary HTML tags <DOCTYPE>,<html>, <head>, <title>, and <body>:

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Topographic Map</title> </head> <body> Hello World </body> </html>

There are a number of flavors of HTML currently in use. Most new HTML pages developed today use HTML5, so we'll focus on HTML5 throughout the book, but without delving into many of its advanced features. Other versions of HTML you are likely to encounter in the wild include HTML 4.0.1 and XHTML 1.0.

DOCTYPE

The first line of your HTML page will contain the DOCTYPE. This is used to tell the browser how the HTML should be interpreted. We'll focus on HTML5 in this book so the first example you see in the following uses the HTML5 DOCTYPE. The two other common doctypes are HTML 4.01 Strict and XHTML 1.0 Strict:

HTML5:

<!DOCTYPE html>

HTML 4.01 Strict:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

XHTML 1.0 Strict:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Primary tags

At a minimum, all your web pages will need to contain the <html>, <head>, and <body> tags. The <html> tag defines the whole HTML document. All other tags must be placed inside this tag. Tags that define how the web page will appear in the browser are placed inside the <body> tag. For instance, your mapping applications will contain a <div> tag inside the <body> tag that is used as a container for displaying the map.

Loading this HTML page in a browser would produce the content you see in the following screenshot. Most of the ArcGIS API for JavaScript code that you write will be placed between the <head></head> tags, either within a <script> tag or inside a separate JavaScript file. As you gain experience you will likely begin placing your JavaScript code inside one or more JavaScript files and then referencing them from the <head> section. We'll explore this topic later. For now just concentrate on placing your code inside <script> tags, within the <head> tags:

Validating HTML code

I've mentioned that it is very important that your HTML tags be coded correctly. This is all well and good you say, but how do I know my HTML has been coded correctly? Well, there are a number of HTML code validators that you can use to check your HTML. The W3C HTML validator (http://validator.w3.org/) shown in the following screenshot can be used to validate HTML code by URI, File Upload, or Direct Input:

Assuming that your HTML code successfully validates you will get a nice screen with a message indicating a successful validation as seen in the following screenshot:

On the other hand, it will identify any problems with a red error message as shown in the following screenshot. Errors are described in detail which makes it easier to correct problems. Often a single error can lead to many other errors so it is not uncommon to see a long list of error items. Don't panic if this is the case. Fixing one error often resolves many others:

To correct the errors in the preceding document you would need to surround the text Hello World with paragraph tags:

<p>Hello World</p>

JavaScript fundamentals

As the name implies, the ArcGIS API for JavaScript requires that you use the JavaScript language when developing your application. There are some fundamental JavaScript programming concepts that you will need to know before starting to build your application.

JavaScript is a lightweight scripting language that is embedded in all modern web browsers. Although JavaScript can certainly exist outside the web browser environment in other applications, it is best known for its integration with web applications.

All modern web browsers including Chrome, Firefox, Safari, and Edge have JavaScript embedded. The use of JavaScript in web applications enables the creation of dynamic applications that do not require round trips to the server to fetch data, and thus the applications are more responsive and feel like native applications. However, JavaScript can submit requests to the server for more (or more up-to-date) information, and is a core technology in the AJAX (Asynchronous JavaScript and XML) stack.

One common misconception regarding JavaScript is that it is actually a simplified version of Java. The two languages are actually unrelated: only the names and C-like syntax are similar.

Commenting code

It is best practice to always document your JavaScript code through the use of comments. At a minimum, this should include the author of the code, date of last revision, and the general purpose of the code. In addition, at various points throughout your code you should include comment sections that define the purpose of specific sections of the application. The purpose of this documentation is to make it easier for you or another programmer to quickly get up to speed in the event that the code needs to be updated in some way.

Any comments that you include in your code are not executed. They are simply ignored by the JavaScript interpreter. JavaScript can be commented in a couple of ways including single line and multi-line comments. Single line comments start with//and any additional characters you add to the line. The following code example shows how single line comments are created:

//this is a single line comment. This line will not be executed

Multi-line comments in JavaScript start with/*and end with*/. Any lines in between are treated as comments and are not executed. The following code example shows an example of multi-line comments (also called block comments):

/*

Copyright 2012 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

*/

Variable data types