search
HomeWeb Front-endJS TutorialEnhance Your ArcGIS Web App with OpenAI

As I’ve progressed through my coding experiences and career (and, let’s be honest, throughout life), I’m constantly looking for fun ways to connect what I learn with what I know. Most recently, I’ve been creating a video series for web developers looking to add a bit of AI to their mapping applications. I’ve had fun testing out different AI libraries and showing developers just how easy it can be to implement these in their apps.

Today, I’m guiding you through a tutorial that shows how to generate insights about a location with minimal code. We’ll be using the OpenAI API alongside the ArcGIS Maps SDK for JavaScript, with the modern web components approach. This is a simple and declarative way to add powerful mapping features to your web apps. Don’t worry if you’re new to either of these tools — I’ll guide you through each step.

AI ArcGIS JS SDK Project Setup

To start, we’ll use a simple HTML, CSS, and JS setup. In Visual Studio Code, I’ll create the HTML, CSS, and JS files. In the HTML, I’ll use the ! Enter shortcut for the basic HTML setup. I’ll also be sure to link my CSS and JS files. Here's what it looks like so far:

    
    
      
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <link rel="stylesheet" href="/styles.css">
        <script src="/scripts.js"></script>
      
      
      
    

HTML Setup

Since I’m using the ArcGIS Maps SDK for JS components, I’ll need to include a couple of libraries through CDN — Calcite Components and the ArcGIS JS SDK. We’re using CDN for simplicity, but for larger production apps, using npm is recommended.

    <link rel="stylesheet" type="text/css" href="https://js.arcgis.com/calcite-components/2.13.2/calcite.css">
    <link rel="stylesheet" href="https://js.arcgis.com/4.31/esri/themes/light/main.css">

    <script type="module" src="https://js.arcgis.com/calcite-components/2.13.2/calcite.esm.js"></script>    
    <script src="https://js.arcgis.com/4.31/"></script>
    <script type="module" src="https://js.arcgis.com/map-components/4.31/arcgis-map-components.esm.js"></script>

Before using the ArcGIS map styles, I’ll set up authentication with my ArcGIS API key. Note: Never expose your API key publicly in the frontend. In a production environment, you should use a backend server to securely handle API requests and manage keys. For now, I’ll include this above all other script tags in my HTML file for simplicity.

    <script>
       var esriConfig = {
         apiKey: "yourKey"
       };
    </script>

Now, I can use the custom element to represent the map and set the basemap, center (longitude, latitude), and zoom properties to my liking.

    <arcgis-map basemap="arcgis/topographic" center="-77.02, 38.89" zoom="13">
      <arcgis-zoom position="top-left"></arcgis-zoom>
    </arcgis-map>

Here’s what the final HTML file should look like:

    
    
      
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>OpenAI + ArcGIS Map</title>

        <script>
          var esriConfig = {
            apiKey: "yourKey"
          };
        </script>

        <link rel="stylesheet" type="text/css" href="https://js.arcgis.com/calcite-components/2.13.2/calcite.css">
        <link rel="stylesheet" href="https://js.arcgis.com/4.31/esri/themes/light/main.css">
        <link rel="stylesheet" href="/styles.css">

        <script type="module" src="https://js.arcgis.com/calcite-components/2.13.2/calcite.esm.js"></script>    
        <script src="https://js.arcgis.com/4.31/"></script>
        <script type="module" src="https://js.arcgis.com/map-components/4.31/arcgis-map-components.esm.js"></script>
      
     
        <arcgis-map basemap="arcgis/topographic" center="-77.02, 38.89" zoom="13">
          <arcgis-zoom position="top-left"></arcgis-zoom>
        </arcgis-map>
        <script src="/scripts.js"></script>
     
    

CSS Setup

Now, onto the CSS. We’ll start by targeting the html, body, and arcgis-map elements. Since I want my map to take up the entire page, I'll ensure there are no predefined padding or margins and that it takes up 100% of the height and width of the page.

    html,
    body,
    arcgis-map {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }

At this point, you’ll be able to see your ArcGIS JS map on the screen. Functionality is limited to zooming and panning, so let’s continue!

Enhance Your ArcGIS Web App with OpenAI

JS Setup

Now, onto the fun part — getting our map to interact with OpenAI! First, we need to define our OpenAI API key for authentication. You’ll need to have an account with OpenAI to get one. After that, we’ll grab the map by selecting the custom element from the DOM and assigning it to the arcgisMap variable. This lets us manipulate the map programmatically.

    
    
      
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <link rel="stylesheet" href="/styles.css">
        <script src="/scripts.js"></script>
      
      
      
    

Speaking of interacting with our map, we’ll go ahead and add an event listener to it. We’ll capture the arcgisViewClick event, which is triggered when the user clicks anywhere on the map. This will help us get the clicked location and send it to the OpenAI API.

After the user clicks, we’ll extract the map coordinates (mapPoint) from the event details and store them in the params object. This gives us the location (longitude, latitude) where the user clicked. We'll also use outFields: "*" to request all attributes for the clicked feature (you can refine this based on your needs).

    <link rel="stylesheet" type="text/css" href="https://js.arcgis.com/calcite-components/2.13.2/calcite.css">
    <link rel="stylesheet" href="https://js.arcgis.com/4.31/esri/themes/light/main.css">

    <script type="module" src="https://js.arcgis.com/calcite-components/2.13.2/calcite.esm.js"></script>    
    <script src="https://js.arcgis.com/4.31/"></script>
    <script type="module" src="https://js.arcgis.com/map-components/4.31/arcgis-map-components.esm.js"></script>

Now, let’s move on to getting the response from OpenAI. First, we define the prompt that will be sent to the API using the longitude and latitude from params.location. Next, we'll destructure the choices array from the response. This is where OpenAI will store the generated content based on our prompt. We'll then call the OpenAI API with fetch(), sending a POST request to the chat/completions endpoint. The headers include Content-Type: application/json to indicate we're sending and receiving JSON, and Authorization with the Bearer token for authentication.

In the body, we specify the model (I’m choosing the gpt-4o model here). Then, we'll pass the prompt in the messages field with a "user" role, indicating it's user input. After making the request, we use await to get the response and call .json() to parse it.

    <script>
       var esriConfig = {
         apiKey: "yourKey"
       };
    </script>

Finally, we use arcgisMap.popup.open() to display a popup at the clicked location. The longitude and latitude are set from params.location, and the title is whatever you choose. The content is set to the AI-generated text from choices[0].message.content, showing the fact on the map.

    <arcgis-map basemap="arcgis/topographic" center="-77.02, 38.89" zoom="13">
      <arcgis-zoom position="top-left"></arcgis-zoom>
    </arcgis-map>

Final App in Action

With everything set up, this app now allows users to click anywhere on the map. Based on their click, it sends the coordinates to the OpenAI API. The API generates a relevant fact about that location, which is then displayed in a popup on the map. But don’t stop there — have fun with it! You can easily change the prompt sent to OpenAI. For example, you could ask for a scary fact ?, a funny one ?, or even ask it to include emojis! ?

For your reference, here’s a CodePen with the full code.

Enhance Your ArcGIS Web App with OpenAI

Conclusion

By following these steps, we’ve successfully created an AI-powered mapping application. Here’s a quick recap of what we’ve done:

  • Set up the map using the ArcGIS Maps SDK for JavaScript Web Components via CDN.

  • Used the OpenAI API to generate dynamic content based on clicked locations.

  • Displayed that content in a popup on the map.

With this approach, you’ve seen how easy it is to integrate AI and mapping tools into your web applications. The key takeaway is that it really takes minimal setup and code to build powerful applications that combine real-time user interactions with AI insights.

So whether you’re enhancing your mapping projects or exploring new ways to use AI, I hope I’ve inspired you today. I’d love to see any future creations you make, so please share them here or with me on my socials. I’d love to see your work!


This article was written by Courtney Yatteau, a Developer Advocate at Esri. The opinions expressed in this article are solely Courtney’s and do not necessarily represent the views, strategies, or opinions of her employer. If you have any feedback, please feel free to like and/or comment. Additionally, if you have any questions or comments that you’d prefer to send privately, you can contact Courtney through LinkedIn, X, Bluesky, or email. If you’re considering a career switch, looking to get into tech, or curious about what it’s like to work at Esri, then please stay tuned for future posts! You can also check out Esri’s careers page or this video for more information.

The above is the detailed content of Enhance Your ArcGIS Web App with OpenAI. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.