當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JavaScript ArcGIS MapView.hitTest用法及代碼示例

基本信息

以下是所在類或對象的基本信息。

AMD: require(["esri/views/MapView"], (MapView) => { /* code goes here */ });

ESM: import MapView from "@arcgis/core/views/MapView";

類: esri/views/MapView

繼承: MapView > View > Accessor

自從:用於 JavaScript 4.0 的 ArcGIS API

用法說明

MapView.hitTest函數(或屬性)的定義如下:

hitTest (screenPoint, options) {Promise<HitTestResult>}


返回與指定屏幕坐標相交的每個圖層的要素。結果被組織為包含帶有 Graphic 的對象的數組。如果命中相交要素,以下圖層類型將返回所有要素: FeatureLayer , CSVLayer , GeoJSONLayer , GeoRSSLayer , GraphicsLayer , KMLLayer , MapNotesLayer , OGCFeatureLayer , StreamLayerWFSLayer

VectorTileLayer 命中測試結果包含一個圖形,其屬性指示與屏幕點相交的vector tile style 內的圖層的 ID 和名稱。不返回有關圖層中表示的實際要素的詳細屬性和空間信息。

發布特定更改:

  • 在 4.23 版中,來自要素圖層的所有命中測試要素都會在結果中返回。在以前的版本中,僅返回最重要的函數。
  • 在 4.6 版中,添加了對 VectorTileLayer 的支持。

參數:

規格:
類型說明
screenPoint ScreenPoint|MouseEvent

單擊視圖的屏幕坐標(或本機鼠標事件)。

options Object
可選的

用於指定 hitTest 中包含或排除的內容的選項。自 4.16 起支持。

規格:
可選的

要包含在 hitTest 中的圖層和圖形列表。如果未指定 include,則將包括所有圖層和圖形。

可選的

要從 hitTest 中排除的圖層和圖形列表。如果未指定排除,則不會排除任何圖層或圖形。

返回:

類型 說明
Promise<HitTestResult> 解析後,返回一個包含與給定屏幕坐標相交的圖形(如果存在)的對象。

例子:

// Get the screen point from the view's click event
view.on("click", function (event) {
  // Search for graphics at the clicked location. View events can be used
  // as screen locations as they expose an x,y coordinate that conforms
  // to the ScreenPoint definition.
  view.hitTest(event).then(function (response) {
    if (response.results.length) {
      let graphic = response.results.filter(function (result) {
        // check if the graphic belongs to the layer of interest
        return result.graphic.layer === myLayer;
      })[0].graphic;

      // do something with the result graphic
      console.log(graphic.attributes);
    }
  });
});
// get screenpoint from view's pointer-move event
view.on("pointer-move", function(event){
  // Search for graphics on layers at the hovered location
  // exclude view.graphics from the hitTest
  view.hitTest(event, {exclude: view.graphics}).then(function(response){
    // if graphics are returned, do something with results
    if (response.results.length){
       // do something
    }
  });
});
// get screenpoint from view's click event
view.on("click", function(event){
  // Search for all features only on included layers at the hovered location
  view.hitTest(event, {include: featureLayer}).then(function(response){
    // if features are returned from the featureLayer, do something with results
    if (response.results.length){
       // do something
       console.log(response.results.length, "features returned");
    }
  })
});

相關用法


注:本文由純淨天空篩選整理自arcgis.com大神的英文原創作品 MapView.hitTest。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。