柿子树备忘录

vuePress-theme-reco 柿子树    2023
柿子树备忘录

Choose mode

  • dark
  • auto
  • light
首页
个人笔记
  • Web
  • GIS
  • Database
  • DevOps
  • 可视化
地图故事
生活点滴
归档
关于我
author-avatar

柿子树

109

Article

73

Tag

首页
个人笔记
  • Web
  • GIS
  • Database
  • DevOps
  • 可视化
地图故事
生活点滴
归档
关于我
  • GIS理论基础

    • GIS基础知识
    • 地图坐标系统
  • GeoServer笔记

    • 思维导图
    • 一、OGC简述

    • 二、基本使用

    • 三、服务标准

    • 四、图层加载

    • 五、服务端开发

  • Openlayers

    • 思维导图
    • 一、快速起步

    • 二、ol结构体系

    • 三、数据源加载

    • 四、常用控件

      • 默认控件
      • 添加控件
      • 常用控件
      • 自定义导航
    • 五、几何对象与Style样式

    • 六、事件交互

    • 七、OGC服务

    • 八、常用示例

  • CesiumJS

    • 思维导图
  • WorldWind

    • WorldWindJava 学习笔记
    • OpenGL中的坐标系

自定义导航

vuePress-theme-reco 柿子树    2023

自定义导航

ac 2020-10-29 OpenLayers

# 自定义导航

来实现一个很久以前比较流行的自定义导航控件。

这需要了解CSS的定位position和background属性的知识,来学习构建这个组件,后续的事件交互就是通过View对象控制地图视图的操作。

image-20201110173336191

材料:

map_view.png

toolbar_rbg.png

完整代码:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="css/ol.css" type="text/css">
    <style>
        .map {
            position: relative;
            height: 400px;
            width: 100%;
        }

        /*自定义导航start*/
        .map-toolbar {
            position: absolute;
            left: 20px;
            top: 20px;
            visibility: visible;
            width: 52px;
            overflow: visible;
            z-index: 150;
        }

        .map-pancontrol {
            position: relative;
            display: block;
            width: 52px;
            height: 52px;
            background: url("./img/map_view.png") 0 175px;
        }

        .map-pan-left, .map-pan-top, .map-pan-right, .map-pan-bottom {
            position: absolute;
            cursor: pointer
        }

        .map-pan-left, .map-pan-right {
            width: 12px;
            height: 18px;
            top: 17px
        }

        .map-pan-top, .map-pan-bottom {
            width: 18px;
            height: 12px;
            left: 17px
        }

        .map-pan-left {
            left: 8px
        }

        .map-pan-right {
            left: 32px
        }

        .map-pan-top {
            top: 8px
        }

        .map-pan-bottom {
            top: 31px
        }

        .map-pan-left:hover,
        .map-pan-top:hover,
        .map-pan-right:hover,
        .map-pan-bottom:hover {
            background: url(./img/map_view.png);
        }

        .map-pan-left:hover {
            background-position: -52px -110px
        }

        .map-pan-top:hover {
            background-position: -70px -112px
        }

        .map-pan-right:hover {
            background-position: -61px -110px
        }

        .map-pan-bottom:hover {
            background-position: -84px -110px
        }

        .map-zoomcontrol {
            position: relative;
            left: 14px;
            width: 24px;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            -o-user-select: none;
            user-select: none
        }

        .map-zoom-plus,
        .map-zoom-minus,
        .map-zoom-cursor {
            background: url(./img/map_view.png);
            cursor: pointer
        }

        .map-zoom-ruler,
        .map-zoom-mask {
            background: url(./img/toolbar_rbg.png);
            cursor: pointer
        }

        .map-zoom-ruler {
            overflow: visible
        }

        .map-zoom-plus,
        .map-zoom-minus {
            width: 24px;
            height: 21px
        }

        .map-zoom-plus {
            background-position: 0 -217px
        }

        .map-zoom-plus:hover {
            background-position: 0 -194px
        }

        .map-zoom-minus {
            background-position: -26px -224px
        }

        .map-zoom-minus:hover {
            background-position: -26px -195px
        }

        .map-zoom-ruler {
            display: block;
            width: 12px;
            height: 147px;
            position: relative;
            left: 6px;
            background-position: 0 0
        }

        .map-zoom-mask,
        .map-zoom-cursor {
            position: absolute
        }

        .map-zoom-mask {
            height: 45px;
            width: 12px;
            background-position: -14px 0
        }

        .map-zoom-cursor {
            top: 45px;
            width: 24px;
            height: 20px;
            left: -6px;
            background-position: -127px -164px
        }

        .map-zoom-cursor:hover {
            background-position: -127px -141px
        }

        .map-locate {
            position: relative;
            left: 17px;
            display: block;
            width: 18px;
            height: 18px;
            background: url(./img/map_view.png) -130px -185px;
            cursor: pointer
        }

        .map-touch-toolbar .map-zoomcontrol {
            position: absolute;
            right: 4px;
            bottom: -80px;
            z-index: 500;
            width: 35px;
            background-color: white;
            background-color: rgba(255, 255, 255, 0.9);
            border-radius: 3px;
            border: 1px solid #ccc;
            box-shadow: 1px 1px 10px 0 #ccc
        }

        .map-touch-toolbar .map-zoomcontrol:after {
            position: absolute;
            content: "";
            height: 1px;
            background: #ddd;
            top: 48px;
            width: 60%;
            margin: auto;
            left: 0;
            right: 0
        }

        /*自定义导航end*/
    </style>
    <script src="lib/ol.js"></script>
    <title>简单控件</title>
</head>
<body>
<h2>自定义导航</h2>
<div id="mouse_position"></div>
<div id="map" class="map">
    <!--自定义导航start -->
    <div class="map-toolbar">
        <div class="map-pancontrol">
            <div class="map-pan-left" onclick="handleClickPan('left')"></div>
            <div class="map-pan-top" onclick="handleClickPan('top')"></div>
            <div class="map-pan-right" onclick="handleClickPan('right')"></div>
            <div class="map-pan-bottom" onclick="handleClickPan('bottom')"></div>
        </div>
        <div class="map-locate" onclick="reposition()"></div>
        <div class="map-zoomcontrol">
            <div class="map-zoom-plus" onclick="handleClickZoom('plus')"></div>
            <div class="map-zoom-ruler">
                <div class="map-zoom-mask"></div>
                <div class="map-zoom-cursor"></div>
            </div>
            <div class="map-zoom-minus" onclick="handleClickZoom('minus')"></div>
        </div>
    </div>
    <!--自定义导航end -->
</div>
<script type="text/javascript">

    var shenzhen = ol.proj.fromLonLat([113.958334, 22.535640]);
    var initZoom = 12;
    var map = new ol.Map({
        view: new ol.View({
            center: shenzhen,
            zoom: initZoom,
            maxZoom: 20,
            minZoom: 1
            // rotation: Math.PI/6
        }),
        layers: [
            new ol.layer.Tile({
                source: new ol.source.OSM()
            })
        ],
        target: "map",
        controls: []//去除默认控件
    });
    var view = map.getView();

    function handleClickPan(type) {
        var oldExtent = view.calculateExtent();
        var height = oldExtent[3] - oldExtent[1];
        var width = oldExtent[2] - oldExtent[0];
        var center = view.getCenter(), newCenter = null;
        switch (type) {
            case "left":
                newCenter = [center[0] - width / 2, center[1]];
                break;
            case "top":
                newCenter = [center[0], center[1] + height / 2];
                break;
            case "right":
                newCenter = [center[0] + width / 2, center[1]];
                break;
            case "bottom":
                newCenter = [center[0], center[1] - height / 2];
        }
        if (center) {
            view.animate({center: newCenter});
        }
    }

    function reposition() {
        view.setCenter(shenzhen);
        view.setZoom(initZoom);
    }

    function handleClickZoom(type) {
        var initZoom = view.getZoom();
        if (type == "plus") {
            view.setZoom(initZoom + 1);
        } else if (type == "minus") {
            view.setZoom(initZoom - 1);
        }
    }

    // ------------缩放条部分-------------
    // 初始化缩放条
    function getCalcMaskHeight() {
        var zoomBarHeight = document.getElementsByClassName("map-zoom-ruler")[0].clientHeight;
        var maskHeight = document.getElementsByClassName("map-zoom-cursor")[0].clientHeight;
        var zoomSilderHeight = zoomBarHeight - maskHeight / 2;

        var maxZoom = view.getMaxZoom();
        var minZoom = view.getMinZoom();
        var currZoom = view.getZoom();
        return (currZoom - minZoom) / (maxZoom - minZoom) * zoomSilderHeight;
    }

    var initMaskHeight = getCalcMaskHeight()
    document.getElementsByClassName("map-zoom-mask")[0].style.height = initMaskHeight + "px";
    document.getElementsByClassName("map-zoom-cursor")[0].style.top = initMaskHeight + "px";

    //缩放条的拖动
    document.getElementsByClassName("map-zoom-cursor")[0].onmousedown = function (evt) {
        var that = this;
        var oEvent = evt || event;
        var disY = oEvent.clientY - parseInt(that.style.top);

        that.onmousemove = function (evt) { //实时改变目标元素obox的位置
            var oEvent = evt || event;
            var curY = oEvent.clientY - disY;
            //修改移动cursor的位置
            if (curY < 0) {
                that.style.top = 1 + 'px';
            } else if (curY > 135) {
                that.style.top = 135 + 'px';
            } else {
                that.style.top = oEvent.clientY - disY + 'px';
            }
            //修改mask的位置
            document.getElementsByClassName("map-zoom-mask")[0].style.height = that.style.top;
            //设置地图
            var zoomBarHeight = document.getElementsByClassName("map-zoom-ruler")[0].clientHeight;
            var maxZoom = view.getMaxZoom();
            var minZoom = view.getMinZoom();
            var newZoom = minZoom + (maxZoom - minZoom) * (parseInt(that.style.top) / zoomBarHeight);
            view.setZoom(newZoom);
        };
        //停止拖动
        that.onmouseup = function () {
            that.onmousemove = null;
            that.onmouseup = null;
        };
        that.onmouseout = function () {
            that.onmousemove = null;
            that.onmouseout = null;
        };
    }
    //绑定视图的缩放事件
    view.on("change:resolution", function changeResolution(e) {
        var maskHeight = getCalcMaskHeight();
        document.getElementsByClassName("map-zoom-mask")[0].style.height = maskHeight + "px";
        document.getElementsByClassName("map-zoom-cursor")[0].style.top = maskHeight + "px";
    })

</script>
</body>
</html>
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355