Tutorial: Display a map

Learn how to use the ArcGIS API for Python and the basemap styles service to display a map.

Web map of Malibu, CA generated with the ArcGIS API for Python map widget.

A map contains layers of geographic data. A map contains a basemap layer and, optionally, one or more data layers. You can display a specific area of a map using a map view and setting the location and zoom level.

This tutorial shows you how to create and display a map of the Santa Monica Mountains in California using the topographic basemap layer.

Prerequisites

The ArcGIS API for Python tutorials use Jupyter Notebooks to execute Python code. If you are new to this environment, please see the guide to install the API and use notebooks locally.

Steps

Get an access token

You need an access token to use the location services used in this tutorial.

  1. Go to the Create an API key tutorial to obtain an access token.
  2. Ensure that the following privilege is enabled: Location services > Basemaps > Basemap styles service
  3. Copy the key as it will be used in a following step.

To learn more about other ways to get an access token, go to Types of authentication.

Import modules and log in

  1. Import the arcgis.gis module. This module is the entry point into the GIS and provides the functionality to manage GIS content, users, and groups.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    from arcgis.gis import GIS
    
    
    
  2. Use the GIS class to log in. Replace YOUR_API_KEY with the API key you previously copied from the developer dashboard as a value to the api_key parameter.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    from arcgis.gis import GIS
    
    
    gis = GIS(api_key="YOUR_API_KEY")
    
    
    

Display the map

  1. Create an instance of the map widget and set the basemap.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    gis = GIS(api_key="YOUR_API_KEY")
    
    
    map = gis.map()
    map.basemap.basemap = "arcgis-streets"
    
    
    
  2. Set the map center and zoom level.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    map = gis.map()
    map.basemap.basemap = "arcgis-streets"
    
    
    map.center = [34.027, -118.805]
    map.zoom = 12
    
    map  # display the map
    
    
  3. Optional: Use the export_to_html method to export the current state of the map widget to a static HTML file which can be viewed in any web browser.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    from os import path, getcwd
    
    export_dir = path.join(getcwd(), "home")
    if not path.isdir(export_dir):
        os.mkdir(export_dir)
    
    export_path = path.join(export_dir, "display-a-map.html")
    
    map.export_to_html(export_path, title="Display a map")

You should see a map with the topographic basemap layer centered on the Santa Monica Mountains in California.

What's next?

Learn how to use additional functionality in these tutorials:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.