Tutorial: Display a map

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

Python mapping widget displaying a map of Malibu, CA

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 API key

To access location services, you need an API key, OAuth 2.0 access token, or ArcGIS Online account. To learn how to create and scope your key, visit the Create an API key tutorial.

  1. Go to your developer dashboard to get an API key.
  2. Copy the key as it will be used in a following step.

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
    
    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
    
    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
    
    gis = GIS(api_key="YOUR_API_KEY")
    
    map = gis.map()
    map.basemap = "topo-vector"
    
    
    
  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
    
    map = gis.map()
    map.basemap = "topo-vector"
    
    
    map.center = [34.027, -118.805]
    map.zoom = 13
    
    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
    
    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.