# 三.控件

# 1.归属

当地图因调整大小而变得太小时,归因将被折叠。这是因为如果地图宽度小于 600 像素,collapsible 选项将设置为 true

查看代码详情
<template>
  <div ref="map" class="map"></div>
</template>

<script>
export default {
  mounted() {
    let {
      Map,
      View,
      layer: { Tile: TileLayer },
      source: { OSM },
      control: { Attribution, defaults: defaultControls },
    } = ol
    const attribution = new Attribution({
      collapsible: false,
    })
    const map = new Map({
      layers: [
        new TileLayer({
          source: new OSM(),
        }),
      ],
      controls: defaultControls({ attribution: false }).extend([attribution]),
      target: this.$refs.map,
      view: new View({
        center: [12579156, 3274244],
        zoom: 2,
      }),
    })
    function checkSize() {
      const small = map.getSize()[0] < 600
      attribution.setCollapsible(small)
      attribution.setCollapsed(small)
    }
    window.addEventListener("resize", checkSize)
    checkSize()
  },
}
</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

# 2.导航控件

查看代码详情
<template>
  <div ref="map" class="map"></div>
</template>

<script>
export default {
  mounted() {
    let {
      Map,
      View,
      layer: { Tile: TileLayer },
      source: { XYZ },
      control: { defaults, ZoomToExtent, Rotate },
    } = ol
    new Map({
      view: new View({
        center: [12579156, 3274244], // 坐标
        zoom: 12, // 放大倍数
        rotation: 0.3, // 旋转弧度
      }),

      layers: [
        new TileLayer({
          source: new XYZ({
            url: `http://wprd0{1-4}.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=6`,
            crossOrigin: "anonymous", //跨域
          }),
        }),
        new TileLayer({
          source: new XYZ({
            url: `http://wprd0{1-4}.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=8`,
            crossOrigin: "anonymous", //跨域
          }),
        }),
      ],
      controls: defaults({
        zoomOptions: {
          zoomInTipLabel: "放大",
          zoomOutTipLabel: "缩小",
        },
        rotateOptions: {
          autoHide: false,
          label: "↑",
          tipLabel: "重置旋转0°",
        },
      }).extend([
        new ZoomToExtent({
          tipLabel: "缩放至广州",
          label: "Z",
          extent: [
            // 视口大小四个点坐标
            12583177, 2639953, 12622469, 2669156,
          ],
        }),
        new Rotate(), // 旋转控件
      ]),
      target: this.$refs.map,
    })
  },
}
</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

# 3.限制范围

查看代码详情
<template>
  <div ref="map" class="map"></div>
</template>

<script>
export default {
  mounted() {
    let {
      Map,
      control: { ZoomSlider, defaults: defaultControls },
      View,
      layer: { Tile: TileLayer },
      source: { OSM },
      style: { Circle: CircleStyle, Fill, Stroke, Style, Text },
    } = ol
    const view = new View({
      center: [328627.563458, 5921296.662223],
      zoom: 8,
      extent: [-572513.341856, 5211017.966314, 916327.095083, 6636950.728974],
    })

    new Map({
      layers: [
        new TileLayer({
          source: new OSM(),
        }),
      ],
      keyboardEventTarget: document,
      target: this.$refs.map,
      view: view,
      controls: defaultControls().extend([new ZoomSlider()]),
    })
  },
}
</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

# 4.全屏控制

查看代码详情
<template>
  <div id="fullscreen" class="fullscreen">
    <div ref="map" class="map"></div>
    <div class="sidepanel">
      <span class="sidepanel-title">侧面板</span>
    </div>
  </div>
</template>

<script>
export default {
  mounted() {
    let {
      Map,
      View,
      layer: { Tile: TileLayer },
      source: { OSM },
      control: { FullScreen, defaults: defaultControls },
    } = ol
    const view = new View({
      center: [-9101767, 2822912],
      zoom: 14,
    })

    const map = new Map({
      controls: defaultControls().extend([
        new FullScreen({
          source: "fullscreen",
        }),
      ]),
      layers: [
        new TileLayer({
          source: new OSM(),
        }),
      ],
      target: this.$refs.map,
      view: view,
    })
  },
}
</script>
<style>
#fullscreen:-webkit-full-screen {
  height: 100%;
  margin: 0;
}
#fullscreen:-ms-fullscreen {
  height: 100%;
}

#fullscreen:fullscreen {
  height: 100%;
}

#fullscreen {
  margin-bottom: 10px;
  width: 100%;
  height: 100%;
}
#fullscreen {
  width: 100%;
  height: 300px;
}
#fullscreen .map .ol-rotate {
  top: 3em;
}

#fullscreen .map {
  width: 80%;
  height: 100%;
  float: left;
}

#fullscreen .sidepanel {
  background: #1f6b75;
  width: 20%;
  height: 100%;
  float: left;
}

#fullscreen .sidepanel-title {
  width: 100%;
  font-size: 3em;
  color: #ffffff;
  display: block;
  text-align: center;
}
</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

# 5.鼠标位置

显示经纬度

查看代码详情
<template>
  <div>
    <div ref="map" class="map"></div>
    <div ref="position"></div>
    <form>
      <label for="projection">坐标系</label>
      <select ref="projection">
        <option value="EPSG:4326">EPSG:4326</option>
        <option value="EPSG:3857">EPSG:3857</option>
      </select>
      <label for="precision">精度</label>
      <input ref="precision" type="number" min="0" max="12" value="4" />
    </form>
  </div>
</template>

<script>
export default {
  mounted() {
    let {
      control: { MousePosition, defaults: defaultControls },
      Map,
      View,
      layer: { Tile: TileLayer },
      source: { OSM },
      coordinate: { createStringXY },
    } = ol;
    const mousePositionControl = new MousePosition({
      coordinateFormat: createStringXY(4),
      projection: "EPSG:4326",
      className: "custom-mouse-position",
      target: this.$refs.position,
    });
    const map = new Map({
      controls: defaultControls().extend([mousePositionControl]),
      layers: [
        new TileLayer({
          source: new OSM(),
        }),
      ],
      target: this.$refs.map,
      view: new View({
        center: [12579156, 3274244],
        zoom: 2,
      }),
    });
    const projectionSelect = this.$refs.projection;
    projectionSelect.addEventListener("change", function (event) {
      mousePositionControl.setProjection(event.target.value);
    });
    const precisionInput = this.$refs.projection;
    precisionInput.addEventListener("change", function (event) {
      const format = createStringXY(event.target.valueAsNumber);
      mousePositionControl.setCoordinateFormat(format);
    });
  },
};
</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

# 6.地图联动

查看代码详情
<template>
  <div class="map-example">
    <div ref="map" class="map"></div>
    <div ref="map1" class="map"></div>
  </div>
</template>

<script>
export default {
  mounted() {
    let {
      Map,
      View,
      layer: { Tile: TileLayer },
      source: { OSM, XYZ },
    } = ol
    const map = new Map({
      view: new View({
        center: [12579156, 3274244], // 坐标
        zoom: 12, // 放大倍数
      }),
      layers: [
        new TileLayer({
          // 创建一个使用Open Street Map地图源的瓦片图层
          name: "矢量图层",
          source: new OSM(),
        }),
      ],
      target: this.$refs.map,
    })
    new Map({
      view: map.getView(),
      layers: [
        new TileLayer({
          name: "卫星图",
          source: new XYZ({
            url: `http://wprd0{1-4}.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=6`,
            crossOrigin: "anonymous", //跨域
          }),
        }),
      ],
      target: this.$refs.map1,
    })
  },
}
</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