基于Openlayers3的WebGIS系统开发,实现如下功能。
- 放大、缩小、漫游、控件加载(鹰眼图、比例尺、缩放按钮);
- 多图层加载、多图层切换显示,其中,要加载显示的图层包括但不限于谷歌地图、OSM地图、GeoJSON矢量数据等。
- 左右地图双窗口联动显示:开发功能按钮,控制右边地图窗口的显示与关闭。
- 图像绘制功能,要求实现点、线、面(矩形、多边形、圆形)等不同图形的绘制。
- 对于面要素图形的绘制,要求能够编写样式设置面板(开发功能按钮,控制该面板的显示与关闭),自定义地设置边框的颜色、宽度,以及填充颜色。
在html文件中写下如下代码:
<html>
<head>
<meta charset="UTF-8">
<title>基于Openlayers3的WebGIS系统开发</title>
<link rel="stylesheet" href="css/WEBGISDZY.css" />
<link rel="stylesheet" href="css/ol.css" />
<script src="js/ol/ol-debug.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery-1.9.1.js" ></script>
<script src="js/WEBGISDZY.js" type="text/javascript"></script>
</head>
<body>
<div id="banner">
<p>基于Openlayers3的WebGIS系统开发</p></div>
<div id="center">
<div id='map'>
<div id="layerControl" class="layerControl">
<div class="title"><label>图层列表</label></div>
<ul id="layerTree" class="layerTree"></ul>
</div>
<div id="drawchoose" class="drawchoose">
<div>确定线宽:<input type="text" id="width" ></div>
<div>确定线颜色:<input type="text" id="color_line" ></div>
<div>确定填充颜色:<input type="text" id="color" ></div>
<div><button id="sure">确定</button></div>
</div>
<div id='btn'>
<button onclick='drawPointEvent()' class='btn1'></button>
<button onclick="drawLineEvent()" class='btn2'></button>
<button onclick="drawRecEvent()" class='btn3'></button>
<button onclick="drawPolygonEvent()" class='btn4'></button>
<button onclick="drawCircleEvent()" class='btn5'></button>
<button onclick="clearDrawEvent()" class='btn6'></button></div>
<div id='btn7'>
<button onclick="mapWin()">联动地图窗口</button></div>
<div id='btn8'>
<button onclick="LayerChoiceSwitch()">选择图层</button></div>
<div><button id="btn9">图形样式设置</button></div>
</div>
<div id='map1'>
</div>
<script src="js/WEBGISDZY.js">
</script>
</div>
<div id="bottom">
<p>基于Openlayers3的WebGIS系统开发</p>
</div>
</body>
</html>
在css文件中写入代码,对页面进行设计装饰。
html,div,ul,li,iframe,p,
img {
padding: 0;
margin: 0;
font-family: "微软雅黑";
}
body{
font-family: "微软雅黑";
background-image:white;
background-size: cover;
}
#banner{width:100%;
height:6%;
background:url(../img/banner.jpg);
top left repeat-x;
}
#banner p{font-size: 30px;
color: white;margin-left: 40%;
margin-top: 15px;
display: inline-block;
letter-spacing: 2px;
}
#center{ padding: 0;
margin: 0;
width: 100%;
height: 91%;
}
#map{
height:850px;
width:50%;
float:left;
display: block;
}
.layerControl {
position: absolute;
bottom: 5px;
min-width: 200px;
max-height: 200px;
right: 50px;
top: 180px;
z-index: 2001;
color: #ffffff;
background-color: #4c4e5a;
border-width: 10px;
border-radius: 10px;
border-color: #000 #000 #000 #000;
}
.layerControl .title {
font-weight: bold;
font-size: 15px;
margin: 10px;
}
.layerTree li {
list-style: none;
margin: 5px 10px;
}
#drawchoose{
position:absolute;
right:50px;
top: 400px;
margin-top:2px;
width: 200px;
height: 170px;
z-index:2003;
color:white;
background-color:#4c4e5a;
border-width: 10px;
border-radius: 10px;
border-color: #000 #000 #000 #000;
display:block;
}
#btn{
right:30px;
position: absolute;
top:110px;
z-index: 2002;
}
.btn1::before{
position: relative;
top: 1px;
bottom:1px;
content: url(../img/DIAN.png);
}
.btn2::before{
position: relative;
top: 1px;
bottom:1px;
content: url(../img/XIAN.png)
}
.btn3::before{
position: relative;
top: 1px;
bottom:1px;
content: url(../img/JUXING.png)
}
.btn4::before{
position: relative;
top: 1px;
bottom:1px;
content: url(../img/DUOBIANX.png)
}
.btn5::before{
position: relative;
top: 1px;
bottom:1px;
content: url(../img/YUANXING.png)
}
.btn6::before{
position: relative;
top: 1px;
bottom:1px;
content: url(../img/QINGCHU.png)
}
#btn7{
right:30px;
position: absolute;
top:150px;
z-index: 2002;
}
#btn8{
right:128px;
position: absolute;
top:150px;
z-index: 2002;
}
#btn9{
right:200px;
position: absolute;
top:150px;
z-index: 2002;
}
#sure{
position: relative;
top: 15px;
left: 80px;
}
#map1{
height:850px;
width:49%;
float:right;
display: block;
}
#bottom{background:blue;height:4%;width:100%;bottom: 0;
position: absolute;text-align: center;}
#bottom p{color: white;bottom: 5px;}
在js文件中写入代码,来控制界面的交互,代码如下:
var map;
//设置绘制样式
var source = new ol.source.Vector();
var Style=new ol.style.Style({
fill:new ol.style.Fill({
color:'rgba(255,255,255,0.6)'
}),
stroke: new ol.style.Stroke({ //边界样式
color: 'rgba(139,0,139,1)',
width: 1.5
}),
image: new ol.style.Circle({
radius: 10,
fill: new ol.style.Fill({
color: '#8A2BE2'
})
})
});
var vector = new ol.layer.Vector({
source: source,
style:Style,
name:'绘制图层',
});
//实现绘制功能
var DrawPoint=new ol.interaction.Draw({
source:source,
type: 'Point',
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.3)'
}),
stroke: new ol.style.Stroke({
color: 'yellow',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: 'yellow'
})
})
})
});
var DrawLine=new ol.interaction.Draw({
source:source,
type: 'LineString',
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.3)'
}),
stroke: new ol.style.Stroke({
color: 'yellow',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: 'yellow'
})
})
})
});
var DrawPolygon=new ol.interaction.Draw({
source:source,
type: 'Polygon',
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.3)'
}),
stroke: new ol.style.Stroke({
color: 'yellow',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: 'yellow'
})
})
})
});
var DrawCircle=new ol.interaction.Draw({
source:source,
type: 'Circle',
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.3)'
}),stroke: new ol.style.Stroke({
color: 'yellow',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: 'yellow'
})
})
})
});
var DrawRec=new ol.interaction.Draw({
source:source,
type: 'LineString',
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.3)'
}),stroke: new ol.style.Stroke({
color: 'yellow',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: 'yellow'
})
})
}),
maxPoints: 2,
geometryFunction: function (coordinates, geometry) {
if (!geometry) {
geometry = new ol.geom.Polygon(null);
}
var start = coordinates[0];
var end = coordinates[1];
geometry.setCoordinates([
[start, [start[0], end[1]], end, [end[0], start[1]], start]
]);
return geometry;
}
});
function drawPointEvent(){
map.removeInteraction(DrawLine);
map.removeInteraction(DrawPolygon);
map.removeInteraction(DrawCircle);
map.removeInteraction(DrawRec);
map.addInteraction(DrawPoint);
}
function drawLineEvent(){
map.removeInteraction(DrawPoint);
map.removeInteraction(DrawPolygon);
map.removeInteraction(DrawCircle);
map.removeInteraction(DrawRec);
map.addInteraction(DrawLine);
}
function drawRecEvent(){
map.removeInteraction(DrawPoint);
map.removeInteraction(DrawLine);
map.removeInteraction(DrawPolygon);
map.removeInteraction(DrawCircle);
map.addInteraction(DrawRec);
}
function drawPolygonEvent(){
map.removeInteraction(DrawPoint);
map.removeInteraction(DrawLine);
map.removeInteraction(DrawCircle);
map.removeInteraction(DrawRec);
map.addInteraction(DrawPolygon);
}
function drawCircleEvent(){
map.removeInteraction(DrawPoint);
map.removeInteraction(DrawLine);
map.removeInteraction(DrawPolygon);
map.removeInteraction(DrawRec);
map.addInteraction(DrawCircle);
}
//实现绘图样式修改功能
document.getElementById("sure").onclick = function(){
var width = document.getElementById('width').value;
var width_color = document.getElementById('color_line').value;
var fill_color = document.getElementById('color').value;
vector.getStyle().getStroke().setWidth(width);
vector.getStyle().getStroke().setColor(width_color);
vector.getStyle().getFill().setColor(fill_color);
}
//实现绘制图形清除
function clearDrawEvent(){
source.clear();
}
//实现副图层的开关
function mapWin(){
if(document.getElementById('map1').style.display=="none"){
document.getElementById('map').style.width="50%";
map.updateSize();
document.getElementById('map1').style.display="block";
}
else{
document.getElementById('map').style.width="100%";
map.updateSize();
document.getElementById('map1').style.display="none";
}
}
//实现图层控制面板
function LayerChoiceSwitch(){
if (document.getElementById('layerTree').style.display=="none"){
document.getElementById('layerTree').style.display="block";
}
else{
document.getElementById('layerTree').style.display="none";
}
}
var layer = new Array(); //map中的图层数组
var layerName = new Array(); //图层名称数组
var layerVisibility = new Array(); //图层可见属性数组
function loadLayersControl(map, id) {
var treeContent = document.getElementById(id); //图层目录容器
var layers = map.getLayers(); //获取地图中所有图层
for(var i = 0; i < layers.getLength(); i++) {
//获取每个图层的名称、是否可见属性
layer[i] = layers.item(i);
layerName[i] = layer[i].get('name');
layerVisibility[i] = layer[i].getVisible();
//新增li元素,用来承载图层项
var elementLi = document.createElement('li');
treeContent.appendChild(elementLi); // 添加子节点
//创建复选框元素
var elementInput = document.createElement('input');
elementInput.type = "checkbox";
elementInput.name = "layers";
elementLi.appendChild(elementInput);
//创建label元素
var elementLable = document.createElement('label');
elementLable.className = "layer";
//设置图层名称
setInnerText(elementLable, layerName[i]);
elementLi.appendChild(elementLable);
//设置图层默认显示状态
if(layerVisibility[i]) {
elementInput.checked = true;
}
addChangeEvent(elementInput, layer[i]); //为checkbox添加变更事件
}
}
/**
* 为checkbox元素绑定变更事件
* @param {input} element checkbox元素
* @param {ol.layer.Layer} layer 图层对象
*/
function addChangeEvent(element, layer) {
element.onclick = function() {
//alert(0);
if(element.checked) {
layer.setVisible(true); //显示图层
} else {
layer.setVisible(false); //不显示图层
}
};
}
/**
* 动态设置元素文本内容(兼容)
*/
function setInnerText(element, text) {
if(typeof element.textContent == "string") {
element.textContent = text;
} else {
element.innerText = text;
}
}
//实现图层控制面板的隐藏显示
$("#btn8").click(function() {
if ($(".layerControl").css("display") == "block") {
$(".layerControl").css("display", "none");
} else {
$(".layerControl").css("display", "block");
}
});
//实现样式设置面板的隐藏显示
$("#btn9").click(function() {
if ($(".drawchoose").css("display") == "block") {
$(".drawchoose").css("display", "none");
} else {
$(".drawchoose").css("display", "block");
}
});
//设置地图,添加控件,实现两个地图窗口联动显示,添加地图控件
var layer1=new ol.layer.Tile({ source: new ol.source.OSM({
url: 'http://{a-c}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png'
}) ,name:'OSM地图'});
var layer2= new ol.layer.Vector({source:new ol.source.Vector({url:'data/countries.geojson',
format:new ol.format.GeoJSON()}),
name:'GeoJSON矢量图'
})
var layer3=new ol.layer.Tile({
source: new ol.source.Stamen({
layer: 'watercolor'
}),
name: 'Stamen地图'
});
var layer4 = new ol.layer.Tile({
source: new ol.source.XYZ({
url: 'http://www.google.cn/maps/vt/pb=!1m4!1m3!1i{z}!2i{x}!3i{y}!2m3!1e0!2sm!3i345013117!3m8!2szh-CN!3scn!5e1105!12m4!1e68!2m2!1sset!2sRoadmap!4e0'
}),
name:'谷歌地图'
});
var View=new ol.View({
center: [0, 0],
zoom: 3
});
map = new ol.Map({
target:'map',
controls:ol.control.defaults().extend([
new ol.control.OverviewMap({
layers: [new ol.layer.Tile({ source: new ol.source.OSM({
url: 'http://{a-c}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png'
}) })],
collapsable:false
}),
new ol.control.ScaleLine({
units: "metric"
}),
new ol.control.MousePosition(),
new ol.control.FullScreen(),
new ol.control.ZoomSlider(),
]),
layers: [
layer1,layer4,layer3,layer2,vector],
view: View
});
map1 = new ol.Map({
target:'map1',
controls:ol.control.defaults().extend([
new ol.control.OverviewMap({
layers: [new ol.layer.Tile({ source: new ol.source.OSM({
url: 'http://{a-c}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png'
}) })],
collapsable:false
}),
new ol.control.ScaleLine({
units: "metric"
}),
new ol.control.MousePosition(),
new ol.control.FullScreen(),
new ol.control.ZoomSlider(),
]),
layers: [layer1,layer4,layer3,layer2,vector],
view: View
});
loadLayersControl(map, "layerTree");
界面设计如下,两个地图窗口,实现地图的放大、缩小、漫游功能,有鹰眼图、比例尺、缩放按钮等控件。还有工具条,图层控制面板,样式设置面板。
使用图标显示按钮内容形成图标。
通过图层控制面板可以选择图层的显示。
左右地图联动显示,通过“联动地图窗口”按钮来控制右边地图的显示与关闭。
实现点、线、面(矩形、多边形、圆形)等不同图形的绘制。
在样式设置面板自定义地设置边框的颜色、宽度,以及填充颜色。
图层列表面板和样式设置面板都可以通过工具条,设置其显示与隐藏。