Add Dojo Button

2181
8
11-17-2017 09:10 AM
BrandonPrice
Occasional Contributor

I would like to add a dojo button using the javascript var mybutton = new button method to the bottom right of a web app inside the map. How do I do that?

Thanks,

Brandon

https://community.esri.com/groups/web-app-builder-custom-widgets?sr=search&searchId=ec47b955-b719-4d...

Tags (3)
0 Kudos
8 Replies
RobertScheitlin__GISP
MVP Emeritus

Brandon,


  This is a pretty broad question. Do you understand the use of placeAt and how to use the style do an adsolute positioning?

0 Kudos
BrandonPrice
Occasional Contributor

Hi Robert,

All I know is the placement of a dojo button at the bottom of a splash page I made. Something like: }).placeAt(splashPage1.containerNode);. I was wondering how to do this in the map itself. I am not sure exactly sure what absolute positioning is. I would guess the placement of the button on the map.

Brandon

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Brandon,

   Here is a sample for adding a button over the map when your dojo layout panes:

<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
  <title>Create web map from id</title>

  <link rel="stylesheet" href="https://js.arcgis.com/3.22/dijit/themes/claro/claro.css">
  <link rel="stylesheet" href="https://js.arcgis.com/3.22/esri/css/esri.css">

  <script src="https://js.arcgis.com/3.22/" data-dojo-config="async:true"></script>
  <script>
    require([
      "dojo/parser",
      "dojo/ready",
      "dijit/layout/BorderContainer",
      "dijit/layout/ContentPane",
      "dojo/dom",
      "esri/map",
      "esri/urlUtils",
      "esri/arcgis/utils",
      "esri/dijit/Legend",
      "esri/dijit/Scalebar",
      "dijit/form/Button",
      "esri/layers/VectorTileLayer",
      "dojo/domReady!"
    ], function(
      parser,
      ready,
      BorderContainer,
      ContentPane,
      dom,
      Map,
      urlUtils,
      arcgisUtils,
      Legend,
      Scalebar,
      Button,
      VectorTileLayer
    ) {
      ready(function() {

        parser.parse();

        //if accessing webmap from a portal outside of ArcGIS Online, uncomment and replace path with portal URL
        //arcgisUtils.arcgisUrl = "https://pathto/portal/sharing/content/items";
        arcgisUtils.createMap("ba7b97b7d6b24c6381eba23062f8aab1", "map").then(function(response) {
          //update the app
          dom.byId("title").innerHTML = response.itemInfo.item.title;
          dom.byId("subtitle").innerHTML = response.itemInfo.item.snippet;

          var map = response.map;

          //add the scalebar
          var scalebar = new Scalebar({
            map: map,
            scalebarUnit: "english"
          });

          //add the legend. Note that we use the utility method getLegendLayers to get
          //the layers to display in the legend from the createMap response.
          var legendLayers = arcgisUtils.getLegendLayers(response);
          var legendDijit = new Legend({
            map: map,
            layerInfos: legendLayers
          }, "legend");
          legendDijit.startup();

          var myButton = new Button({
            label: "Toggle Basemap",
            onClick: function(){
              var baseLyr = map.getLayer(map.layerIds[0]);
              if(baseLyr.url === "https://www.arcgis.com/sharing/rest/content/items/0e0aa048cb9a42de91ae287fc5632fac/resources/styles/..."){
                map.removeLayer(baseLyr);
                var vectorTileLayer = new VectorTileLayer("http://www.arcgis.com/sharing/rest/content/items/e19e9330bf08490ca8353d76b5e2e658/resources/styles/r...");
                map.addLayer(vectorTileLayer, 0);
              } else if(baseLyr.url === "http://www.arcgis.com/sharing/rest/content/items/e19e9330bf08490ca8353d76b5e2e658/resources/styles/r..." ||
                "http://public.gis.lacounty.gov/public/rest/services/LACounty_Cache/LACounty_Base/MapServer"){
                map.removeLayer(baseLyr);
                var vectorTileLayer2 = new VectorTileLayer("http://www.arcgis.com/sharing/rest/content/items/0e0aa048cb9a42de91ae287fc5632fac/resources/styles/r...");
                map.addLayer(vectorTileLayer2, 0);
              }
            },
            style: "position: absolute; top: 20px; right: 20px;"
          }).placeAt(dijit.byId("map").containerNode).startup();
        });
      });

    });
  </script>
  <style>
    html,
    body {
      height: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
    }

    body {
      font-family: "Helvetica";
    }

    #header {
      background-color: #E8E8E8;
      height: 65px;
      margin: 5px 5px;
    }

    #mainWindow {
      width: 100%;
      height: 100%;
    }

    #title {
      padding-top: 2px;
      padding-left: 10px;
      font-size: 18pt;
      font-weight: 700;
    }

    #subtitle {
      font-size: small;
      padding-left: 40px;
    }

    #rightPane {
      background-color: #E8E8E8;
      margin: 5px;
      width: 20%;
    }

    #map {
      margin: 5px;
      padding: 0;
    }
  </style>
</head>

<body class="claro">
  <div id="mainWindow" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline'">
    <div id="header" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'top'">
      <div id="title"></div>
      <div id="subtitle"></div>
    </div>
    <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'">
    </div>
    <div id="rightPane" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'right'">
      <div id="legend"></div>
    </div>
  </div>
</body>

</html>
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Brandon,

   Did my sample help?

BrandonPrice
Occasional Contributor

Hi Robert,

Yes. This is a great sample I will incorporate in the future on other projects. Sorry for the trouble. For this project, which is  a storymap: https://caltrans.maps.arcgis.com/apps/MapSeries/index.html?appid=9c9b080c67af4d2db74b663efba971bd (that is the AGOL version; the other is on a non-public facing server), I am still figuring out a basemap toggle solution. I found that my basemap layer on all maps in the story is app.map.getLayersVisibleAtScale()[0]. Which is the Iayer:

https://www.arcgis.com/sharing/rest/content/items/0e0aa048cb9a42de91ae287fc5632fac/resources/styles/...

in your sample. I would like a solution that uses the arcgis api 3.22 basemap toggle using a basemaptoggle.on selection change or something like:

baseMapToggle = new BasemapToggle({
map: app.map,
basemap: "myImagery",
toggle: function () {
if (isWorldView) {
isWorldView = false;
// switch to Imagery
app.map.getLayer(app.map.layerIds[1]).setVisibility(false);
app.map.getLayer(app.map.layerIds[0]).setVisibility(true);
} else {
isWorldView = true;
// switch to world view
app.map.getLayer(app.map.layerIds[0]).setVisibility(false);
app.map.getLayer(app.map.layerIds[1]).setVisibility(true);
}
}
}, "BasemapToggle");
baseMapToggle.startup();

that toggles the other layer in your sample. Any examples on turning off layers using something like app.map.getLayer(app.map.layerIds[0]).setVisibility(false); with a basemap toggle?

Brandon

0 Kudos
BrandonPrice
Occasional Contributor

Sorry for the late response by the way. I was trying ways on getting it to work with no luck and then got interrupted by the holidays.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Brandon,

   I am not able to get the basemap toggle dijit to work with your vector layers.

0 Kudos
BrandonPrice
Occasional Contributor

Hi Robert,

No problem. I appreciate the example. I will use the in the future in other apps.

-Brandon

0 Kudos