# 十二.Interactions(二)

# 1.禁用功能

禁用功能点 说明
鼠标/手指双击缩放 DoubleClickZoom 鼠标或手指双击缩放地图
鼠标/手指拖拽平移 DragPan 鼠标或手指拖拽平移地图
键盘 + / - 缩放 KeyboardZoom 使用键盘 + 和 - 按键进行缩放
方向键平移 KeyboardPan 使用键盘方向键平移地图
鼠标滚轮缩放 MouseWheelZoom 鼠标滚轮缩放地图
查看代码详情
<template>
  <div ref="map" class="map"></div>
</template>

<script>
export default {
  mounted() {
    let {
      interaction: { DoubleClickZoom, DragPan, MouseWheelZoom, defaults },
      Map,
      View,
      layer: { Tile: TileLayer },
      source: { OSM },
    } = ol
    const map = new Map({
      view: new View({
        center: [12579156, 3274244],
        zoom: 12,
      }),
      layers: [
        new TileLayer({
          source: new OSM(),
        }),
      ],
      target: this.$refs.map,
    })
    // 删除默认的双击事件
    const dblClickInteraction = map
      .getInteractions()
      .getArray()
      .find((interaction) => {
        return interaction instanceof DoubleClickZoom
      })
    map.removeInteraction(dblClickInteraction)
    // 删除拖拽平移事件
    const dragPanInteraction = map
      .getInteractions()
      .getArray()
      .find((interaction) => {
        return interaction instanceof DragPan
      })
    map.removeInteraction(dragPanInteraction)
    // 删除鼠标滚轮缩放事件
    const mouseWheelZoomInteraction = map
      .getInteractions()
      .getArray()
      .find((interaction) => {
        return interaction instanceof MouseWheelZoom
      })
    map.removeInteraction(mouseWheelZoomInteraction)
  },
}
</script>
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

# 2.addfeatures

使用拖放交互与图像矢量渲染的示例。

查看代码详情
<template>
  <div>
    <div ref="map" class="map"></div>
    <div id="info">信息:&nbsp;</div>
  </div>
</template>

<script>
export default {
  mounted() {
    let {
      interaction: { DragAndDrop, defaults: defaultInteractions },
      Map,
      View,
      layer: { Tile: TileLayer, VectorImage: VectorImageLayer },
      source: { XYZ, Vector: VectorSource },
      format: { GPX, GeoJSON, IGC, KML, TopoJSON },
    } = ol;
    const dragAndDropInteraction = new DragAndDrop({
      formatConstructors: [GPX, GeoJSON, IGC, KML, TopoJSON],
    });
    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({
      interactions: defaultInteractions().extend([dragAndDropInteraction]),
      layers: [
        new TileLayer({
          source: new XYZ({
            attributions: attributions,
            url:
              "https://api.maptiler.com/tiles/satellite/{z}/{x}/{y}.jpg?key=" +
              mapkeys.maptiler,
            maxZoom: 20,
          }),
        }),
      ],
      target: this.$refs.map,
      view: new View({
        center: [12579156, 3274244],
        zoom: 2,
      }),
    });
    dragAndDropInteraction.on("addfeatures", function (event) {
      const vectorSource = new VectorSource({
        features: event.features,
      });
      map.addLayer(
        new VectorImageLayer({
          source: vectorSource,
        })
      );
      map.getView().fit(vectorSource.getExtent());
    });
    const displayFeatureInfo = function (pixel) {
      const features = [];
      map.forEachFeatureAtPixel(pixel, function (feature) {
        features.push(feature);
      });
      if (features.length > 0) {
        const info = [];
        let i, ii;
        for (i = 0, ii = features.length; i < ii; ++i) {
          info.push(features[i].get("name"));
        }
        document.getElementById("info").innerHTML = info.join(", ") || "&nbsp";
      } else {
        document.getElementById("info").innerHTML = "&nbsp;";
      }
    };
    map.on("pointermove", function (evt) {
      if (evt.dragging) {
        return;
      }
      const pixel = map.getEventPixel(evt.originalEvent);
      displayFeatureInfo(pixel);
    });
    map.on("click", function (evt) {
      displayFeatureInfo(evt.pixel);
    });
  },
};
</script>
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