使用效果高亮显示国家

尝试一下在线预览

此示例在多个图层上使用图层效果图层混合的组合,以使一个国家在地图的其余部分中脱颖而出。当应用程序加载时,您会看到法国国家将从其他国家中脱颖而出。然后,用户可以单击任何国家以在地图上突出显示该国家。

工作原理

我们首先将以下图层添加到地图中。 我们在图形图层上将 blendMode 设置为 destination-in,并将其添加到组图层中的世界图像切片图层的顶部。然后,我们在组图层下方添加另一个世界图像切片图层实例。

                                  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const worldImagery = new TileLayer({
  portalItem: {
    id: "10df2279f9684e4a9f6a7f08febac2a9" // world imagery
  }
});

// clicked country feature will be added to this layer
const graphicsLayer = new GraphicsLayer({
  blendMode: "destination-in",
  title: "layer"
});

const tileLayer = new TileLayer({
  portalItem: {
    // bottom layer in the group layer
    id: "10df2279f9684e4a9f6a7f08febac2a9" // world imagery
  }
});

// this grouplayer has two layers
// destination-in blendMode set on the graphics layer
// country from the world imagery layer will show when user clicks on a country
const groupLayer = new GroupLayer({
  layers: [
    tileLayer,
    // world imagery layer will show where it overlaps with the graphicslayer
    graphicsLayer
  ],
  opacity: 0 // initially this layer will be transparent
});

const map = new Map({
  layers: [worldImagery, groupLayer]
});

When t当应用程序加载或用户点击地图中的任何国家时,我们调用 highlightCountry 函数(如下所示)。该函数查询国家要素图层并返回点击的国家要素。然后,应用程序会将此功能添加到图形层。由于图形图层设置了 destination-in blendMode,应用程序将在图形图层的边界内显示世界图像平铺图层的内容。我们将 drop-shadowbrightness 效果应用于图层组,以使单击的国家/地区脱颖而出。至于显示在图层组下方的另一个世界影像地图,我们将效果设置为 blur、降低 brightness 和应用 grayscale

                                    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
async function highlightCountry(query, zoomGeometry){
  // country symbol - when user clicks on a country
  // we will query the country from the countries featurelayer
  // add the country feature to the graphics layer.
  const symbol = {
    type: "simple-fill",
    color: "rgba(255, 255, 255, 1)",
    outline: null
  }

  // query the countries layer for a country that intersects the clicked point
  const {
      features: [feature]
    } = await countries.queryFeatures(query);
    // user clicked on a country and the feature is returned
    if (feature) {
      graphicsLayer.graphics.removeAll();
      feature.symbol = symbol;
      // add the country to the graphics layer
      graphicsLayer.graphics.add(feature);
      // zoom to the highlighted country
      view.goTo(
        {
          target: zoomGeometry,
          extent: feature.geometry.extent.clone().expand(1.8)
        },
        { duration: 1000 }
      );
      // blur the world imagery basemap so that the clicked country can be highlighted
      worldImagery.effect = "blur(8px) brightness(1.2) grayscale(0.8)";
      // set the group layer opacity to 1
      // also increase the layer brightness and add drop-shadow to make the clicked country stand out.
      groupLayer.effect = "brightness(1.5) drop-shadow(0, 0px, 12px)";
      groupLayer.opacity = 1;
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.