[GIS] Displaying static and dynamic content on top of Map using ArcGIS API for JavaScript

arcgis-javascript-api

Using ArcGIS Javascript API, we have a custom widget to allow user to select his role to analyse the data on the Map. I must display the "Role : XXXX" on top right of the Map. This should be visible to the user always. "Role: " is static & "XXXX" is dynamic as and when user changes his role. I tried using TextSymbol to display the content on the Map. TextSymbol requires "Graphic point" to be added to Graphics Layer.

var t = new TextSymbol("Role: ", font, color);
var p = new Point([-100,52]);
var g = new Graphic(p,t);
graphicsLayer.add(g);
map.addLayer(graphicsLayer);

By giving the point in graphics, the text moves along with the map as the point becomes fixed.

How do I add text to Map without map point?

Best Answer

If you just want to place some text on top of the map div, then you could add a div with absolute positioning. No need to use the API for this.

Example code: https://jsbin.com/mijariy/edit?html,output. Look for the roleInfo div, and the CSS for it.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>rolediv on top of map</title>
  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }

    #roleInfo {
      top: 20px;
      left: 100px;
      position: absolute;
      z-index: 99;
      border-radius: 8px;
      border: 2px solid gray;
      color: gray;
      background: white;
      padding: 10px;
      opacity: 0.65;
    }
  </style>

  <link rel="stylesheet" href="https://js.arcgis.com/4.3/esri/css/main.css">
  <script src="https://js.arcgis.com/4.3/"></script>

  <script>
    require([
      "esri/Map",
      "esri/views/MapView",
      "dojo/domReady!"
    ], function(Map, MapView) {

      var map = new Map({
        basemap: "streets"
      });

      var view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: 4,
        center: [15, 65]
      });

    });
  </script>
</head>

<body>
  <div id="roleInfo">Role: <span id="role">TBD</span></div>
  <div id="viewDiv"></div>
</body>
</html>