OpenLayers基础教程——常规的地图控件

1、前言

熟悉GIS的同志对地图控件应该不会陌生,ArcMap中就有很多地图控件,比如放大、缩小、全图等。其实在OpenLayers中也有很多地图控件,它们都继承了ol.control.Control,下面就来一一介绍。

2、缩放——Zoom

缩放控件是OpenLayers默认就加载的,它对应的是ol.Control.Zoom,如下图所示:
在这里插入图片描述

3、属性——Attribution

属性控件也是OpenLayers默认就加载的,一般在地图右下角,主要是显示版权信息,它对应的是ol.Control.Attribution,如下图所示:
在这里插入图片描述

4、旋转——Rotate

旋转控件其实也是OpenLayers默认就加载的,一般在地图右上角,但如果不在代码中进行设置的话,我们是无法看见它的,它对应的是ol.control.Rotate,代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>OpenLayers</title>
    <link href="lib/ol/ol.css" rel="stylesheet" />
    <script src="lib/ol/ol.js"></script>
</head>
<body>
    <div id="map" style="width:400px;height:400px;"></div>
    <script>
        var map = new ol.Map({
            target: 'map',
            layers: [
                new ol.layer.Tile({
                    source: new ol.source.OSM()
                })
            ],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [120, 30],
                zoom: 10
            }),
            controls: ol.control.defaults().extend([
                new ol.control.Rotate({
                    autoHide: false
                })
            ])
        })
    </script>
</body>
</html>

运行结果如下所示:在这里插入图片描述
操作时,同时按住Shift、Alt,然后利用鼠标进行地图旋转即可,如要复位地图,则点击右上角的复位按钮即可。

5、全屏显示——FullScreen

全屏显示控件一般在地图右上角,它对应的是ol.control.FullScreen,但需要注意:IE浏览器不支持该功能,代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>OpenLayers</title>
    <link href="lib/ol/ol.css" rel="stylesheet" />
    <script src="lib/ol/ol.js"></script>
</head>
<body>
    <div id="map" style="width:400px;height:400px;"></div>
    <script>
        var map = new ol.Map({
            target: 'map',
            layers: [
                new ol.layer.Tile({
                    source: new ol.source.OSM()
                })
            ],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [120, 30],
                zoom: 10
            }),
            controls: ol.control.defaults().extend([
                new ol.control.FullScreen()
            ])
        })
    </script>
</body>
</html>

运行结果如下所示:
在这里插入图片描述

6、缩放滑块——ZoomSlider

缩放滑块对应的是ol.control.ZoomSlider,代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>OpenLayers</title>
    <link href="lib/ol/ol.css" rel="stylesheet" />
    <script src="lib/ol/ol.js"></script>
</head>
<body>
    <div id="map" style="width:400px;height:400px;"></div>
    <script>
        var map = new ol.Map({
            target: 'map',
            layers: [
                new ol.layer.Tile({
                    source: new ol.source.OSM()
                })
            ],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [120, 30],
                zoom: 10
            }),
            controls: ol.control.defaults().extend([
                new ol.control.ZoomSlider()
            ])
        })
    </script>
</body>
</html>

运行结果如下所示:
在这里插入图片描述

7、缩放至某范围——ZoomToExtent

该控件对应的是ol.control.ZoomToExtent,代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>OpenLayers</title>
    <link href="lib/ol/ol.css" rel="stylesheet" />
    <script src="lib/ol/ol.js"></script>
</head>
<body>
    <div id="map" style="width:400px;height:400px;"></div>
    <script>
        var map = new ol.Map({
            target: 'map',
            layers: [
                new ol.layer.Tile({
                    source: new ol.source.OSM()
                })
            ],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [120, 30],
                zoom: 10
            }),
            controls: ol.control.defaults().extend([
                new ol.control.ZoomToExtent({
                    extent: [110, 10, 120, 30]
                })
            ])
        })
    </script>
</body>
</html>

运行结果如下所示:
在这里插入图片描述

8、比例尺——ScaleLine

比例尺控件对应的是ol.control.ScaleLine,代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>OpenLayers</title>
    <link href="lib/ol/ol.css" rel="stylesheet" />
    <script src="lib/ol/ol.js"></script>
</head>
<body>
    <div id="map" style="width:400px;height:400px;"></div>
    <script>
        var map = new ol.Map({
            target: 'map',
            layers: [
                new ol.layer.Tile({
                    source: new ol.source.OSM()
                })
            ],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [120, 30],
                zoom: 10
            }),
            controls: ol.control.defaults().extend([
                new ol.control.ScaleLine({
                    units: 'metric'
                })
            ])
        })
    </script>
</body>
</html>

运行结果如下所示:
在这里插入图片描述
比例尺控件的单位默认是metric,当然你也可以设置成其他的单位,比如degrees、us、nautical等等。

9、鹰眼——OverviewMap

鹰眼控件对应的是ol.control.OverviewMap,代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>OpenLayers</title>
    <link href="lib/ol/ol.css" rel="stylesheet" />
    <script src="lib/ol/ol.js"></script>
</head>
<body>
    <div id="map" style="width:400px;height:400px;"></div>
    <script>
        var map = new ol.Map({
            target: 'map',
            layers: [
                new ol.layer.Tile({
                    source: new ol.source.OSM()
                })
            ],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [120, 30],
                zoom: 10
            }),
            controls: ol.control.defaults().extend([
                new ol.control.OverviewMap({
                    layers: [
                        new ol.layer.Tile({
                            source: new ol.source.OSM()
                        })
                    ],
                    view: new ol.View({
                        projection: 'EPSG:4326'
                    })
                })
            ])
        })
    </script>
</body>
</html>

运行结果如下所示:
在这里插入图片描述
需要注意的是,鹰眼地图的投影需要跟主视图保持一致,否则显示会有问题。

10、加载地图控件的另一种写法

在上面的代码中,我们都是将控件写在一个数组中,其实我们也可以采用下面这种写法:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>OpenLayers</title>
    <link href="lib/ol/ol.css" rel="stylesheet" />
    <script src="lib/ol/ol.js"></script>
</head>
<body>
    <div id="map" style="width:400px;height:400px;"></div>
    <script>
        var map = new ol.Map({
            target: 'map',
            layers: [
                new ol.layer.Tile({
                    source: new ol.source.OSM()
                })
            ],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [120, 30],
                zoom: 10
            })
        })

        // ZoomSlider
        var zoomSlider = new ol.control.ZoomSlider();
        map.addControl(zoomSlider);

        // ZoomToExtent
        var zoomToExtent = new ol.control.ZoomToExtent();
        map.addControl(zoomToExtent);

        // FullScreen
        var fullScreen = new ol.control.FullScreen();
        map.addControl(fullScreen);

        // ScaleLine
        var scaleLine = new ol.control.ScaleLine({
            units: 'metric'
        });
        map.addControl(scaleLine);
    </script>
</body>
</html>

运行结果如下所示:
在这里插入图片描述

11、禁用控件

由于OpenLayers自带的地图控件样式可能与现有系统不匹配,因此很多时候我们会自己做一个浮动工具条实现相关功能,此时就需要把一些默认加载的控件禁用掉。禁用控件很简单,代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>OpenLayers</title>
    <link href="lib/ol/ol.css" rel="stylesheet" />
    <script src="lib/ol/ol.js"></script>
</head>
<body>
    <div id="map" style="width:400px;height:400px;"></div>
    <script>
        var map = new ol.Map({
            target: 'map',
            layers: [
                new ol.layer.Tile({
                    source: new ol.source.OSM()
                })
            ],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [120, 30],
                zoom: 10
            }),
            controls: ol.control.defaults({
                attribution: false,
                zoom: false,
                rotate: false
            })
        })
    </script>
</body>
</html>

运行结果如下所示:
在这里插入图片描述

12、结语

这篇博客主要介绍了OpenLayers中常规的一些地图控件,其实我这里故意漏掉了一个控件,那就是鼠标位置控件——MousePosition。之所以把它留着是因为在下一篇博客中我会介绍如何利用自定义的css样式配合OpenLayers的地图控件进行显示,考虑再三,最后还是决定用MousePosition作为例子进行介绍。OpenLayers的地图控件其实不难,主要就是一些属性的设置,大家好好理解一下应该是能够快速上手的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值