# 九.Overlays 属性

# 1.id

设置叠加 ID。叠加 ID 可以与 module:ol/Map~Map#getOverlayById 方法一起使用

# 2.element

属性 作用 配置
element 叠加元素 -
autoPan 调用 setPosition 时平移地图,以便叠加窗口在当前视口中完全可见?如果 true(已弃用),则将使用 autoPanAnimation 和 autoPanMargin 来确定平移参数;如果提供了对象,则忽略其他参数 -
查看代码详情
<template>
  <div>
    <div ref="map" class="map"></div>
    <div ref="popup" class="ol-popup">
      <a href="#" ref="popupcloser" class="ol-popup-closer"></a>
      <div ref="popupcontent"></div>
    </div>
  </div>
</template>

<script>
export default {
  mounted() {
    let {
      Overlay,
      coordinate: { toStringHDMS },
      Map,
      View,
      layer: { Tile: TileLayer },
      source: { XYZ },
      proj: { toLonLat },
    } = ol;
    const container = this.$refs.popup;
    const content = this.$refs.popupcontent;
    const closer = this.$refs.popupcloser;
    const overlay = new Overlay({
      element: container,
      autoPan: {
        animation: {
          duration: 250,
        },
      },
    });
    closer.onclick = function () {
      overlay.setPosition(undefined);
      closer.blur();
      return false;
    };
    const attributions =
      '<a href="https://www.maptiler.com/copyright/" target="_blank">&copy; MapTiler</a> ' +
      '<a href="https://www.openstreetmap.org/copyright" target="_blank">&copy; OpenStreetMap contributors</a>';
    const map = new Map({
      layers: [
        new TileLayer({
          source: new XYZ({
            attributions: attributions,
            url:
              "https://api.maptiler.com/maps/streets/{z}/{x}/{y}.png?key=" +
              mapkeys.maptiler,
            tileSize: 512,
          }),
        }),
      ],
      overlays: [overlay],
      target: this.$refs.map,
      view: new View({
        center: [12579156, 3274244],
        zoom: 2,
      }),
    });
    map.on("singleclick", function (evt) {
      const coordinate = evt.coordinate;
      const hdms = toStringHDMS(toLonLat(coordinate));
      content.innerHTML = "<p>点击位置</p><code>" + hdms + "</code>";
      overlay.setPosition(coordinate);
    });
  },
};
</script>
<style>
.ol-popup {
  position: absolute;
  background-color: white;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
  padding: 15px;
  border-radius: 10px;
  border: 1px solid #cccccc;
  bottom: 12px;
  left: -50px;
  min-width: 280px;
}
.ol-popup:after,
.ol-popup:before {
  top: 100%;
  border: solid transparent;
  content: " ";
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;
}
.ol-popup:after {
  border-top-color: white;
  border-width: 10px;
  left: 48px;
  margin-left: -10px;
}
.ol-popup:before {
  border-top-color: #cccccc;
  border-width: 11px;
  left: 48px;
  margin-left: -11px;
}
.ol-popup-closer {
  text-decoration: none;
  position: absolute;
  top: 2px;
  right: 8px;
}
.ol-popup-closer:after {
  content: "✖";
}
</style>
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

# 3.offset

定位叠加窗口时使用的像素偏移量。数组中的第一个元素是水平偏移量。正值会向右移动叠加。数组中的第二个元素是垂直偏移量。正值会向下移动叠加窗口

# 4.position

地图投影中的叠加位置

# 5.positioning

Defines how the overlay is actually positioned with respect to its position property. Possible values are 'bottom-left', 'bottom-center', 'bottom-right', 'center-left', 'center-center', 'center-right', 'top-left', 'top-center', and 'top-right'

# 6.stopEvent

事件是否应该停止传播到地图视口。如果 true,则叠加被放置在与控件相同的容器中(CSS 类名 ol-overlaycontainer-stopevent);如果 false,则放置在由 className 属性指定的 CSS 类名的容器中

# 7.insertFirst

覆盖层是首先插入覆盖容器,还是附加。如果叠加窗口与控件的容器相同(请参阅 stopEvent 选项),您可能会将 insertFirst 设置为 true,以便覆盖层显示在控件下方

# 9.autoPanAnimation

用于将叠加窗口移到视图中的动画选项。此动画仅在启用 autoPan 时使用。可以提供 duration 和 easing 来自定义动画。如果 autoPan 作为对象提供,则不建议使用和忽略

# 10.autoPanMargin

自动平移时地图叠加和边框之间的边距(以像素为单位)。如果 autoPan 作为对象提供,则不建议使用和忽略

# 11.autoPanOptions

用于 autoPan 的选项。这仅在启用 autoPan 时使用,并且优先于单个 autoPanMargin 和 autoPanOptions

# 12.className

CSS 类名