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
的地图控件其实不难,主要就是一些属性的设置,大家好好理解一下应该是能够快速上手的。