【问题标题】:change arcgis js api popup click event to right click将 arcgis js api 弹出单击事件更改为右键单击
【发布时间】:2020-06-24 20:54:24
【问题描述】:

使用 arcgis js api 4.15 调用弹出点击事件非常简单,例如您只需定义它。

如下:

    fl = new FeatureLayer({
      source: gras,
      objectIdField: "ObjectID",
      geometryType: "polygon",
      fields: [{
        name: "ObjectID",
        alias: "ObjectID",
        type: "oid"
      }, {
        name: "id",
        alias: "id",
        type: "string"
      }, {
        name: "place",
        alias: "place",
        type: "string"
      }, {
        name: "url",
        alias: "url",
        type: "string"
      }, {
        name: "grid_value",
        alias: "grid_value",
        type: "double"
      }],
      renderer: renderer,
      popupEnabled: true, <------------------------ here
      popupTemplate: popuptemp <---------------------- here
    });

问题是......我想知道是否有人对如何处理将其更改为 API 中的右键单击事件有所了解?

(即缺少文档https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup.html

开箱即用的事件是在图层被单击时触发,我想更改或自定义它以在右键单击时触发事件。

通过他们的文档尝试了另一个逻辑,不确定如何处理嵌套的开放逻辑 - 或者如何从那里调用它。

view.popuptemp.autoOpenEnabled = false;
        view.on("click", function(event) {
           if  (event.which==3) {
                alert('Right mouse button pressed');
                break;
            }
          view.popuptemp.open({
            // Set the popup
          });
        });

【问题讨论】:

    标签: javascript arcgis-js-api


    【解决方案1】:

    如前所述,您必须禁用弹出窗口自动打开,然后在发生右键单击时显示功能信息。这样的东西应该可以工作,

    view.popup.autoOpenEnabled = false; // <- disable view popup auto open
    view.on("click", function (event) { // <- listen to view click event
      if (event.button === 2) { // <- check that was right button
        view.popup.open({ // <- open popup
          location: event.mapPoint, // <- use map point of the click event
          fetchFeatures: true // <- fetch the selected features (if any)
        });
      }
    });
    

    您已经在上面处理了 ArcGIS 示例之一,

    <!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>PopupTemplate - use functions to set content - 4.15</title>
    
      <link rel="stylesheet" href="https://js.arcgis.com/4.15/esri/themes/light/main.css" />
      <script src="https://js.arcgis.com/4.15/"></script>
    
      <style>
        html,
        body,
        #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
        }
      </style>
    
      <script>
        var populationChange;
        require(["esri/Map", "esri/views/MapView", "esri/layers/Layer"], function (
          Map,
          MapView,
          Layer
        ) {
          var map = new Map({
            basemap: "dark-gray"
          });
    
          var view = new MapView({
            container: "viewDiv",
            map: map,
            zoom: 7,
            center: [-87, 34]
          });
    
          var highlightSelect = null;
    
          Layer.fromPortalItem({
            portalItem: {
              id: "e8f85b4982a24210b9c8aa20ba4e1bf7"
            }
          }).then(function (layer) {
            map.add(layer);
    
            var popupTemplate = {
              title: "Population in {NAME}",
              outFields: ["*"],
              content: populationChange,
              fieldInfos: [
                {
                  fieldName: "POP2010",
                  format: {
                    digitSeparator: true,
                    places: 0
                  }
                },
                {
                  fieldName: "POP10_SQMI",
                  format: {
                    digitSeparator: true,
                    places: 2
                  }
                },
                {
                  fieldName: "POP2013",
                  format: {
                    digitSeparator: true,
                    places: 0
                  }
                },
                {
                  fieldName: "POP13_SQMI",
                  format: {
                    digitSeparator: true,
                    places: 2
                  }
                }
              ]
            };
            layer.popupTemplate = popupTemplate;
            function populationChange(feature) {
              var div = document.createElement("div");
              var upArrow =
                '<svg width="16" height="16" ><polygon points="14.14 7.07 7.07 0 0 7.07 4.07 7.07 4.07 16 10.07 16 10.07 7.07 14.14 7.07" style="fill:green"/></svg>';
              var downArrow =
                '<svg width="16" height="16"><polygon points="0 8.93 7.07 16 14.14 8.93 10.07 8.93 10.07 0 4.07 0 4.07 8.93 0 8.93" style="fill:red"/></svg>';
    
              var diff =
                feature.graphic.attributes.POP2013 -
                feature.graphic.attributes.POP2010;
              var pctChange = (diff * 100) / feature.graphic.attributes.POP2010;
              var arrow = diff > 0 ? upArrow : downArrow;
    
              div.innerHTML =
                "As of 2010, the total population in this area was <b>" +
                feature.graphic.attributes.POP2010 +
                "</b> and the density was <b>" +
                feature.graphic.attributes.POP10_SQMI +
                "</b> sq mi. As of 2013, the total population was <b>" +
                feature.graphic.attributes.POP2013 +
                "</b> and the density was <b>" +
                feature.graphic.attributes.POP13_SQMI +
                "</b> sq mi. <br/> <br/>" +
                "Percent change is " +
                arrow +
                "<span style='color: " +
                (pctChange < 0 ? "red" : "green") +
                ";'>" +
                pctChange.toFixed(3) +
                "%</span>";
              return div;
            }
    
            view.popup.autoOpenEnabled = false; // <- disable view popup auto open
            view.on("click", function (event) { // <- listen to view click event
              if (event.button === 2) { // <- check that was right button
                view.popup.open({ // <- open popup
                  location: event.mapPoint, // <- use map point of the click event
                  fetchFeatures: true // <- fetch the selected features (if any)
                });
              }
            });
          });
        });
      </script>
    </head>
    
    <body>
      <div id="viewDiv"></div>
    </body>
    
    </html>

    【讨论】:

    • dang,你是唯一一个懂 arcgis js 的人!大声笑非常感谢这些帮助
    • 为什么要在这里写这个额外的函数:-function populationChange(feature) { ?
    • 有两种“动态”生成弹出内容的方法,使用函数(如示例)或使用承诺。当您需要解决某些任务或添加更多“复杂”内容时,它非常有用。很高兴您发现有用的答案。
    • 如果你想在内容中添加额外的数据,我认为你必须定义一个自定义内容。在那里,使用可达变量应该没有任何问题。如果您遇到问题,请告诉我。
    • 嗨@docholiday,我给了你一个答案,让我知道它是否适合你。
    猜你喜欢
    • 2013-04-04
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 2013-06-28
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多