This tutorial will walk you through how to create a simple map in a 2D MapView.
1. Reference the ArcGIS Maps SDK for JavaScript
First, set up a basic HTML document similar to the following example:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>Intro to MapView - Create a 2D map</title>
</head>
</html>
Inside the <head
tags, reference the ArcGIS Maps SDK for JavaScript using <script
and <link
tags:
<link rel="stylesheet" href="https://js.arcgis.com/4.31/esri/themes/light/main.css" />
<script src="https://js.arcgis.com/4.31/"></script>
The <script
tag loads the ArcGIS Maps SDK for JavaScript from a CDN. When new versions of the API are released, update the version number to match the newly released version.
The <link
tag references the main.css
style sheet which contains styles specific to Esri widgets and components.
2. Load the modules
Use a second <script
tag to load specific modules from the API. Load the following modules using the syntax in the snippet below:
esri/
- loads code specific to creating a mapMap esri/views/
- loads code that allows for viewing the map in 2DMap View
<script>
require([ "esri/Map", "esri/views/MapView" ], (Map, MapView) => {
// Code to create the map and view will go here
});
</script>
Putting JavaScript inside a script tag is useful when building simple pages or experimenting, but not suitable for larger applications. When building a larger application, all JavaScript should be in separate .js
files.
The global require()
function is used to load the API's AMD modules. To learn more about the API's different modules visit the Overview Guide page.
3. Create the map
A new map is created using Map
, which is a reference to the Map class that was loaded from the esri/
module. You can specify map properties, such as basemap
, by passing an object to the Map constructor.
require(["esri/Map", "esri/views/MapView"], (Map, MapView) => {
const map = new Map({
basemap: "topo-vector"
});
});
Additional basemap options are: satellite
, hybrid
, gray-vector
, dark-gray-vector
, oceans
, streets-vector
, osm
, national-geographic
, streets-night-vector
. Use alternate basemaps by modifying the basemap
option in the sandbox. View the Map class for more details on additional map options.
4. Create a 2D view
Views reference nodes that serve as containers in HTML files, allowing users to view the map inside an HTML page. Create a new Map
and set its properties by passing an object to its constructor:
require(["esri/Map", "esri/views/MapView"], (Map, MapView) => {
const map = new Map({
basemap: "topo-vector"
});
const view = new MapView({
container: "viewDiv", // Reference to the DOM node that will contain the view
map: map // References the map object created in step 3
});
});
In this snippet, we set the container
property to the name of the DOM node that will hold the map, in this case we use the id
attribute of the <div
element. The map
property references the map object we created in the previous step. See the MapView documentation for additional properties you may set on the view, including center
and zoom
, which may be used to define the initial extent of the view.
There are two types of views available: MapView (used for viewing 2D maps) and SceneView (used for viewing 3D maps). Click here to learn more about creating maps with 3D views.
5. Define the page content
Now the JavaScript needed to create a map and a view is complete! The next step is to add the associated HTML for viewing the map. For this example, the HTML is very simple: add a <body
tag, which defines what is visible in the browser, and a single <div
element inside the body where the view will be created.
<body>
<div id="viewDiv"></div>
</body>
The <div
has an id
of "viewDiv" to match the id
that is passed to the MapView's container
property in the constructor.
6. Style the page
Style the content of the page using <style
tags inside the <head
. To make the map fill the browser window, add the following CSS inside the page's <style
:
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>
You've now built your first web application in 2D using the ArcGIS Maps SDK for JavaScript 4.31! Your final HTML code should look like the following:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>Intro to MapView - Create a 2D map</title>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>
<link rel="stylesheet" href="https://js.arcgis.com/4.31/esri/themes/light/main.css" />
<script src="https://js.arcgis.com/4.31/"></script>
<script>
require(["esri/Map", "esri/views/MapView"], (Map, MapView) => {
const map = new Map({
basemap: "topo-vector"
});
const view = new MapView({
container: "viewDiv", // Reference to the view div created in step 5
map: map, // Reference to the map object created before the view
zoom: 4, // Sets zoom level based on level of detail (LOD)
center: [15, 65] // Sets center point of view using longitude,latitude
});
});
</script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>