BasemapToggle在ArcGIS JavaScript 4.x中的切换事件不起作用。

huangapple go评论98阅读模式
英文:

BasemapToggle on toggle event not working in ArcGIS Javascript 4.x

问题

以下是翻译好的内容,代码部分未翻译:

"The following code worked is suppose to work in ArcGIS Javascript API 4.x:

basemapToggle.on('toggle', function(event){
console.log("current basemap title: ", event.current.title);
console.log("previous basemap title: ", event.previous.title);
});

Here is the reference:

https://totalapis.github.io/api-reference/esri-widgets-BasemapToggle.html

But the event never occurs. Any ideas what could be happening?"

英文:

The following code worked is suppose to work in ArcGIS Javascript API 4.x:

basemapToggle.on('toggle', function(event){
  console.log("current basemap title: ", event.current.title);
  console.log("previous basemap title: ", event.previous.title);
});

Here is the reference:

https://totalapis.github.io/api-reference/esri-widgets-BasemapToggle.html

But the event never occurs. Any ideas what could be happening?

答案1

得分: 2

以下是您要翻译的内容:

I do not think that works, at least the official API docs (ArcGIS JS API - BasemapToggle) do not specify any event for BasemapToggle.

But, if you want to know when the toggle occurs, you can listen changes on the property activeBasemap.

Here I take an ESRI example (ArcGIS JS Samples - Intro to widgets using BasemapToggle) and modify it to show you that,

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;meta
      name=&quot;viewport&quot;
      content=&quot;initial-scale=1,maximum-scale=1,user-scalable=no&quot;
    /&gt;
    &lt;title&gt;
      Intro to widgets using BasemapToggle | Sample | ArcGIS Maps SDK for
      JavaScript 4.26
    &lt;/title&gt;

    &lt;link
      rel=&quot;stylesheet&quot;
      href=&quot;https://js.arcgis.com/4.26/esri/themes/light/main.css&quot;
    /&gt;
    &lt;script src=&quot;https://js.arcgis.com/4.26/&quot;&gt;&lt;/script&gt;

    &lt;style&gt;
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    &lt;/style&gt;

    &lt;script&gt;
      require([
        &quot;esri/Map&quot;,
        &quot;esri/views/MapView&quot;,
        &quot;esri/widgets/BasemapToggle&quot;
      ], (Map, MapView, BasemapToggle) =&gt; {
        // Create the Map with an initial basemap
        const map = new Map({
          basemap: &quot;topo-vector&quot;
        });

        // Create the MapView and reference the Map in the instance
        const view = new MapView({
          container: &quot;viewDiv&quot;,
          map: map,
          center: [-86.049, 38.485],
          zoom: 3
        });

        // 1 - Create the widget
        const toggle = new BasemapToggle({
          // 2 - Set properties
          view: view, // view that provides access to the map's 'topo-vector' basemap
          nextBasemap: &quot;hybrid&quot; // allows for toggling to the 'hybrid' basemap
        });

        // ----------------------------
        // Watch changes of the basemap
        toggle.watch('activeBasemap', function (basemap) {
            console.log("current basemap title: ", basemap.title);
        });
        // ----------------------------

        // Add widget to the top right corner of the view
        view.ui.add(toggle, 'top-right');
      });
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;viewDiv&quot;&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

(注意:上述内容包括代码,仅提供文本翻译。)

英文:

I do not think that works, at least the official API docs (ArcGIS JS API - BasemapToggle) do not specify any event for BasemapToggle.

But, if you want to know when the toggle occurs, you can listen changes on the property activeBasemap.

Here I take an ESRI example (ArcGIS JS Samples - Intro to widgets using BasemapToggle) and modify it to show you that,

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;meta
      name=&quot;viewport&quot;
      content=&quot;initial-scale=1,maximum-scale=1,user-scalable=no&quot;
    /&gt;
    &lt;title&gt;
      Intro to widgets using BasemapToggle | Sample | ArcGIS Maps SDK for
      JavaScript 4.26
    &lt;/title&gt;

    &lt;link
      rel=&quot;stylesheet&quot;
      href=&quot;https://js.arcgis.com/4.26/esri/themes/light/main.css&quot;
    /&gt;
    &lt;script src=&quot;https://js.arcgis.com/4.26/&quot;&gt;&lt;/script&gt;

    &lt;style&gt;
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    &lt;/style&gt;

    &lt;script&gt;
      require([
        &quot;esri/Map&quot;,
        &quot;esri/views/MapView&quot;,
        &quot;esri/widgets/BasemapToggle&quot;
      ], (Map, MapView, BasemapToggle) =&gt; {
        // Create the Map with an initial basemap
        const map = new Map({
          basemap: &quot;topo-vector&quot;
        });

        // Create the MapView and reference the Map in the instance
        const view = new MapView({
          container: &quot;viewDiv&quot;,
          map: map,
          center: [-86.049, 38.485],
          zoom: 3
        });

        // 1 - Create the widget
        const toggle = new BasemapToggle({
          // 2 - Set properties
          view: view, // view that provides access to the map&#39;s &#39;topo-vector&#39; basemap
          nextBasemap: &quot;hybrid&quot; // allows for toggling to the &#39;hybrid&#39; basemap
        });

        // ----------------------------
        // Watch changes of the basemap
        toggle.watch(&#39;activeBasemap&#39;, function (basemap) {
            console.log(&quot;current basemap title: &quot;, basemap.title);
        });
        // ----------------------------

        // Add widget to the top right corner of the view
        view.ui.add(toggle, &quot;top-right&quot;);
      });
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;viewDiv&quot;&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 23:14:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定