在一个图层中查找多个特征,在鼠标定位下,弧形图示在图层中。

问题描述 投票:0回答:1

我想查看一个图形层中所有与鼠标的x,y相匹配的特征。hitTest()方法对最上面的一个点有效。

        mapView.on("pointer-move", function(event) {        // HANDLE HOVER
            let screenPoint={ x: event.x, y: event.y }; 
            mapView.hitTest(screenPoint).then(function(response) {
                if (response.results.length) {
                     DO SOMETHING...
                     }
                });

但是,当我放大时,这些点会重叠成一个。我怎么知道还有其他的点,并能访问它们?

Google Earth曾经有一个功能可以自动将它们显示在一个圆圈中。如果arcGis也有这个功能,那就太好了,但我还是想硬着头皮做。

arcgis arcgis-js-api
1个回答
0
投票

在新版本的API中,4.x,通常是一个 "指针移动 "的方法。GraphicLayer 的目的(处理客户端功能)成为了 FeatureLayer 或其他层,如 GeoJSONLayerCSVLayer.

现在,建议使用 FeatureLayer,

在处理客户端图形时,通常首选使用其源属性构建一个FeatureLayer,因为FeatureLayer比GraphicsLayer具有更多的功能,包括渲染、查询和标记。

ArcGIS JavaScript API - GraphicLayer

在可视化方面,你可以用聚类对 FeatureLayer.

看一下我给你做的这个例子,是基于 ArcGIS JavaScript示例 - 点聚类.

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

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

    <script>
      require([
        "esri/Map",
        "esri/views/MapView",
        "esri/layers/FeatureLayer",
        "esri/Graphic",
        "esri/geometry/Point",
        "esri/widgets/Legend",
        "esri/widgets/Expand"
      ], function(
        Map,
        MapView,
        FeatureLayer,
        Graphic,
        Point,
        Legend,
        Expand
      ) {
        function getRandomNumber(min, ref) {
          return Math.random() * ref + min;
        }

        function getGraphics() {
          const graphics = [];
          let location = null;
          // generate random points features
          let oid = 0;
          for (let i = 0; i <= getRandomNumber(20, 50); i++) {
            location = new Point({
              latitude: getRandomNumber(10, 50),
              longitude: -getRandomNumber(50, 50)
            });
            for (let j = 0; j <= getRandomNumber(0, 50); j++) {
              graphics.push(
                new Graphic({
                  geometry: location,
                  attributes: {
                    OBJECTID: oid,
                    name: `${i}-${j}`
                  }
                })
              );
              oid++;
            }
          }
          return graphics;
        }

        const graphics = getGraphics();

        function popupTemplateContent(feature) {
          const location = feature.graphic.geometry;
          return `lat:${location.latitude.toFixed(2)} lon:${location.longitude.toFixed(2)}`;
        }

        const clusterConfig = {
          type: "cluster",
          clusterRadius: "100px",
          popupTemplate: {
            content: "{cluster_count} features."
          }
        };

        function createLayer() {
          return new FeatureLayer({
            source: graphics,
            objectIdField: "OBJECTID",
            fields: [
              {
                name: "OBJECTID",
                type: "oid"
              },
              {
                name: "name",
                type: "string"
              }
            ],
            featureReduction: clusterConfig,
            popupTemplate: {
              title: '{name}',
              content: popupTemplateContent
            },
            renderer: {
              type: "simple",
              field: "mag",
              symbol: {
                type: "simple-marker",
                size: 4,
                color: "#fc3232",
                outline: {
                  color: [50, 50, 50]
                }
              }
            }
          });
        }

        const layer = createLayer();

        const view = new MapView({
          map: new Map({
            basemap: "gray-vector"
          }),
          container: "viewDiv",
          zoom: 2,
          center: [-75, 35]
        });

        view
          .when()
          .then(addLayerToView)
          .catch(function(e) {
            console.error("Creating FeatureLayer failed", e);
          });

        function addLayerToView() {
          view.map.add(layer);
        }

        const legend = new Legend({
          view: view,
          container: "legendDiv"
        });

        const infoDiv = document.getElementById("infoDiv");
        view.ui.add(
          new Expand({
            view: view,
            content: infoDiv,
            expandIconClass: "esri-icon-layer-list",
            expanded: true
          }),
          "top-left"
        );

        const toggleButton = document.getElementById("cluster");

        toggleButton.addEventListener("click", function() {
          const fr = layer.featureReduction;
          layer.featureReduction =
            fr && fr.type === "cluster" ? null : clusterConfig;
          toggleButton.innerText =
            toggleButton.innerText === "Enable Clustering"
              ? "Disable Clustering"
              : "Enable Clustering";
        });
      });
    </script>
  </head>
  <body>
    <div id="viewDiv"></div>
    <div id="infoDiv" class="esri-widget">
      <button id="cluster" class="esri-button">Disable Clustering</button>
      <div id="legendDiv"></div>
    </div>
  </body>
</html>

在这个例子中,当你选择聚类是否开启时,它将显示有多少个特征,如果聚类关闭,你将得到所有的特征。

© www.soinside.com 2019 - 2024. All rights reserved.