1、前言

前面的博客其实已经介绍过了如何在openlayers中绘制图形,不过那是基于已有的坐标进行绘制。很多时候,用户往往需要使用鼠标自行在地图上进行图形绘制,这就涉及到了openlayers中的地图交互功能,下面开始介绍。

2、绘制基础图形

openlayers中,实现地图交互的类是ol.interaction,默认情况下,openlayers只支持Point(点)LineString(线)Polygon(面)Circle(圆)这四种基本图形。下面一段代码实现了这四种基本图形的绘制:

<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta charset="utf-8" /><title>OpenLayers</title><style>html, body, #map {width: 100%;height: 100%;margin: 0;padding: 0;}#group {position: absolute;right: 20px;top: 20px;z-index: 200;}</style><!-- openlayers --><link href="libs/ol/ol.css" rel="stylesheet" /><script src="libs/ol/ol.js"></script><!-- bootstrap --><link href="libs/bootstrap/css/bootstrap.min.css" rel="stylesheet" /><script src="libs/bootstrap/js/jquery-3.4.1.min.js"></script><script src="libs/bootstrap/js/bootstrap.min.js"></script>
</head>
<body><div id="map"><div id="group" class="btn-group"><button id="point" type="button" class="btn btn-primary">画点</button><button id="lineString" type="button" class="btn btn-success">画线</button><button id="polygon" type="button" class="btn btn-warning">画面</button><button id="circle" type="button" class="btn btn-info">画圆</button><button id="stop" type="button" class="btn btn-danger">结束</button></div></div><script>// 创建图层var source = new ol.source.Vector();var layer = new ol.layer.Vector({source: source});// 创建地图var map = new ol.Map({target: 'map',layers: [new ol.layer.Tile({source: new ol.source.OSM()}),layer],view: new ol.View({projection: 'EPSG:4326',center: [120, 30],zoom: 8})});//var draw;// 绘制点$('#point').click(function () {if (draw != undefined && draw != null) {map.removeInteraction(draw);}draw = new ol.interaction.Draw({source: source,type: 'Point',style: new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})})})});map.addInteraction(draw);})// 绘制线$('#lineString').click(function () {if (draw != undefined && draw != null) {map.removeInteraction(draw);}draw = new ol.interaction.Draw({source: source,type: 'LineString',style: new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'yellow',width: 2})})});map.addInteraction(draw);})// 绘制面$('#polygon').click(function () {if (draw != undefined && draw != null) {map.removeInteraction(draw);}draw = new ol.interaction.Draw({source: source,type: 'Polygon',style: new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'yellow',width: 2}),fill: new ol.style.Fill({color: 'blue'})})});map.addInteraction(draw);})// 绘制圆$('#circle').click(function () {if (draw != undefined && draw != null) {map.removeInteraction(draw);}draw = new ol.interaction.Draw({source: source,type: 'Circle',style: new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'yellow',width: 2}),fill: new ol.style.Fill({color: 'blue'})})});map.addInteraction(draw);})// 结束绘制$('#stop').click(function () {if (draw != undefined && draw != null) {map.removeInteraction(draw);}})</script>
</body>
</html>

运行结果如下图所示:

3、设置图形样式

我们也可以对绘制的图形的样式进行设置,还是老方法:创建layer时指定样式即可。代码如下:

<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta charset="utf-8" /><title>OpenLayers</title><style>html, body, #map {width: 100%;height: 100%;margin: 0;padding: 0;}#group {position: absolute;right: 20px;top: 20px;z-index: 200;}</style><!-- openlayers --><link href="libs/ol/ol.css" rel="stylesheet" /><script src="libs/ol/ol.js"></script><!-- bootstrap --><link href="libs/bootstrap/css/bootstrap.min.css" rel="stylesheet" /><script src="libs/bootstrap/js/jquery-3.4.1.min.js"></script><script src="libs/bootstrap/js/bootstrap.min.js"></script>
</head>
<body><div id="map"><div id="group" class="btn-group"><button id="point" type="button" class="btn btn-primary">画点</button><button id="lineString" type="button" class="btn btn-success">画线</button><button id="polygon" type="button" class="btn btn-warning">画面</button><button id="circle" type="button" class="btn btn-info">画圆</button><button id="stop" type="button" class="btn btn-danger">结束</button></div></div><script>// 创建图层var source = new ol.source.Vector();var layer = new ol.layer.Vector({source: source,style: function (feature, resolution) {var style = new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'green',width: 2}),fill: new ol.style.Fill({color: 'skyblue'})});return style;}});// 创建地图var map = new ol.Map({target: 'map',layers: [new ol.layer.Tile({source: new ol.source.OSM()}),layer],view: new ol.View({projection: 'EPSG:4326',center: [120, 30],zoom: 8})});//var draw;// 绘制点$('#point').click(function () {if (draw != undefined && draw != null) {map.removeInteraction(draw);}draw = new ol.interaction.Draw({source: source,type: 'Point',style: new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})})})});map.addInteraction(draw);})// 绘制线$('#lineString').click(function () {if (draw != undefined && draw != null) {map.removeInteraction(draw);}draw = new ol.interaction.Draw({source: source,type: 'LineString',style: new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'yellow',width: 2})})});map.addInteraction(draw);})// 绘制面$('#polygon').click(function () {if (draw != undefined && draw != null) {map.removeInteraction(draw);}draw = new ol.interaction.Draw({source: source,type: 'Polygon',style: new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'yellow',width: 2}),fill: new ol.style.Fill({color: 'blue'})})});map.addInteraction(draw);})// 绘制圆$('#circle').click(function () {if (draw != undefined && draw != null) {map.removeInteraction(draw);}draw = new ol.interaction.Draw({source: source,type: 'Circle',style: new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'yellow',width: 2}),fill: new ol.style.Fill({color: 'blue'})})});map.addInteraction(draw);})// 绘制预案$('#stop').click(function () {if (draw != undefined && draw != null) {map.removeInteraction(draw);}})</script>
</body>
</html>

运行结果如下图所示:

4、设置捕捉距离

假设现在绘制一个四边形,那么有两种方式可以结束绘制:1、画到第四个点时双击鼠标2、第四个点绘制完后回到第一个点,使之成为一个封闭图形。如果是用第二种方式,我们可以设置捕捉距离,以实现精确绘制,代码如下:

<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta charset="utf-8" /><title>OpenLayers</title><style>html, body, #map {width: 100%;height: 100%;margin: 0;padding: 0;}#group {position: absolute;right: 20px;top: 20px;z-index: 200;}</style><!-- openlayers --><link href="libs/ol/ol.css" rel="stylesheet" /><script src="libs/ol/ol.js"></script><!-- bootstrap --><link href="libs/bootstrap/css/bootstrap.min.css" rel="stylesheet" /><script src="libs/bootstrap/js/jquery-3.4.1.min.js"></script><script src="libs/bootstrap/js/bootstrap.min.js"></script>
</head>
<body><div id="map"><div id="group" class="btn-group"><button id="polygon" type="button" class="btn btn-primary">画面</button></div></div><script>// 创建图层var source = new ol.source.Vector();var layer = new ol.layer.Vector({source: source,style: function (feature, resolution) {var style = new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'green',width: 2}),fill: new ol.style.Fill({color: 'skyblue'})});return style;}});// 创建地图var map = new ol.Map({target: 'map',layers: [new ol.layer.Tile({source: new ol.source.OSM()}),layer],view: new ol.View({projection: 'EPSG:4326',center: [120, 30],zoom: 8})});//var draw;// 绘制面$('#polygon').click(function () {if (draw != undefined && draw != null) {map.removeInteraction(draw);}draw = new ol.interaction.Draw({source: source,type: 'Polygon',style: new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'yellow',width: 2}),fill: new ol.style.Fill({color: 'blue'})}),snapTolerance: 40});map.addInteraction(draw);})</script>
</body>
</html>

我在这里将捕捉的距离设置为40像素,大家可以试一试,如果用第二种方式进行绘制,最后一个点会被捕捉移到第一个点的位置。

5、绘制三角形

使用上面的代码其实可以完成三角形的绘制,但我们还有另一个方法,那就是使用maxPoints属性,该属性规定了绘制的多边形最多包含几个点,这样就能帮我们自动完成三角形的绘制,代码如下:

<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta charset="utf-8" /><title>OpenLayers</title><style>html, body, #map {width: 100%;height: 100%;margin: 0;padding: 0;}#group {position: absolute;right: 20px;top: 20px;z-index: 200;}</style><!-- openlayers --><link href="libs/ol/ol.css" rel="stylesheet" /><script src="libs/ol/ol.js"></script><!-- bootstrap --><link href="libs/bootstrap/css/bootstrap.min.css" rel="stylesheet" /><script src="libs/bootstrap/js/jquery-3.4.1.min.js"></script><script src="libs/bootstrap/js/bootstrap.min.js"></script>
</head>
<body><div id="map"><div id="group" class="btn-group"><button id="triangle" type="button" class="btn btn-primary">画三角形</button></div></div><script>// 创建图层var source = new ol.source.Vector();var layer = new ol.layer.Vector({source: source,style: function (feature, resolution) {var style = new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'green',width: 2}),fill: new ol.style.Fill({color: 'greenyellow'})});return style;}});// 创建地图var map = new ol.Map({target: 'map',layers: [new ol.layer.Tile({source: new ol.source.OSM()}),layer],view: new ol.View({projection: 'EPSG:4326',center: [120, 30],zoom: 8})});var draw;// 绘制三角形$('#triangle').click(function () {if (draw != undefined && draw != null) {map.removeInteraction(draw);}draw = new ol.interaction.Draw({source: source,type: 'Polygon',style: new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'yellow',width: 2}),fill: new ol.style.Fill({color: 'blue'})}),maxPoints: 3});map.addInteraction(draw);})</script>
</body>
</html>

运行结果如下图所示:

6、绘制正多边形

正多边形是以为基础进行绘制的图形,下面代码实现了正三角形正方形正五边形正六边形的绘制:

<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta charset="utf-8" /><title>OpenLayers</title><style>html, body, #map {width: 100%;height: 100%;margin: 0;padding: 0;}#group {position: absolute;right: 20px;top: 20px;z-index: 200;}</style><!-- openlayers --><link href="libs/ol/ol.css" rel="stylesheet" /><script src="libs/ol/ol.js"></script><!-- bootstrap --><link href="libs/bootstrap/css/bootstrap.min.css" rel="stylesheet" /><script src="libs/bootstrap/js/jquery-3.4.1.min.js"></script><script src="libs/bootstrap/js/bootstrap.min.js"></script>
</head>
<body><div id="map"><div id="group" class="btn-group"><button type="button" class="btn btn-primary" onclick="drawRegularPolygon(3)">三角形</button><button type="button" class="btn btn-success" onclick="drawRegularPolygon(4)">正方形</button><button type="button" class="btn btn-warning" onclick="drawRegularPolygon(5)">正五边形</button><button type="button" class="btn btn-info" onclick="drawRegularPolygon(6)">正六边形</button><button type="button" class="btn btn-danger" onclick="removeDraw()">结束</button></div></div><script>// 创建图层var source = new ol.source.Vector();var layer = new ol.layer.Vector({source: source,style: function (feature, resolution) {var style = new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'green',width: 2}),fill: new ol.style.Fill({color: 'Crimson'})});return style;}});// 创建地图var map = new ol.Map({target: 'map',layers: [new ol.layer.Tile({source: new ol.source.OSM()}),layer],view: new ol.View({projection: 'EPSG:4326',center: [120, 30],zoom: 8})});// 绘制正多边形var draw;function drawRegularPolygon(pointNumber) {if (draw != undefined && draw != null) {map.removeInteraction(draw);}draw = new ol.interaction.Draw({source: source,type: 'Circle',style: new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'yellow',width: 2}),fill: new ol.style.Fill({color: 'blue'})}),geometryFunction: ol.interaction.Draw.createRegularPolygon(pointNumber)});map.addInteraction(draw);}// 绘制预案function removeDraw() {if (draw != undefined && draw != null) {map.removeInteraction(draw);}}</script>
</body>
</html>

运行结果如下图所示:

7、绘制矩形

矩形的绘制较为特殊,需要用到maxPointsgeometryFunction这两个属性,同时以LineString的形式进行绘制,代码如下:

<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta charset="utf-8" /><title>OpenLayers</title><style>html, body, #map {width: 100%;height: 100%;margin: 0;padding: 0;}#group {position: absolute;right: 20px;top: 20px;z-index: 200;}</style><!-- openlayers --><link href="libs/ol/ol.css" rel="stylesheet" /><script src="libs/ol/ol.js"></script><!-- bootstrap --><link href="libs/bootstrap/css/bootstrap.min.css" rel="stylesheet" /><script src="libs/bootstrap/js/jquery-3.4.1.min.js"></script><script src="libs/bootstrap/js/bootstrap.min.js"></script>
</head>
<body><div id="map"><div id="group" class="btn-group"><button id="rectangle" type="button" class="btn btn-primary">绘制矩形</button></div></div><script>// 创建图层var source = new ol.source.Vector();var layer = new ol.layer.Vector({source: source,style: function (feature, resolution) {var style = new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'green',width: 2}),fill: new ol.style.Fill({color: 'DarkViolet'})});return style;}});// 创建地图var map = new ol.Map({target: 'map',layers: [new ol.layer.Tile({source: new ol.source.OSM()}),layer],view: new ol.View({projection: 'EPSG:4326',center: [120, 30],zoom: 8})});var draw;// 绘制正三角形$('#rectangle').click(function () {if (draw != undefined && draw != null) {map.removeInteraction(draw);}draw = new ol.interaction.Draw({source: source,type: 'LineString',style: new ol.style.Style({image: new ol.style.Circle({radius: 10,fill: new ol.style.Fill({color: 'red'})}),stroke: new ol.style.Stroke({color: 'yellow',width: 2}),fill: new ol.style.Fill({color: 'blue'})}),maxPoint: 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;}});map.addInteraction(draw);})</script>
</body>
</html>

运行结果如下图所示:

8、结语

openlayers中,基础图形的绘制很简单,只需要设置不同的type即可。而特殊的图形,诸如正方形矩形等则需要借助ol.interaction.Draw中的一些属性和方法实现。

OpenLayers基础教程——地图交互之绘制图形相关推荐

  1. openlayers调用高德地图web服务绘制驾车路线规划

    openlayers调用高德地图web服务绘制驾车路线规划 使用ol.geom.Polygon()函数将坐标点连接成线时,只连接数组中首末两点的坐标,是因为数组中的值并非number类型,需要将其进行 ...

  2. Reportlab基础教程03之如何绘制线圆椭圆扇形正方形(含代码)

    绘制线 # drawing_lines.pyfrom reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas ...

  3. css 渐变 椭圆,CSS图形基础:利用径向渐变绘制图形

    1.径向渐变 radial-gradient() 函数用于创建一个径向渐变的"图像",其一般调用格式为: background-image: radial-gradient(sha ...

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

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

  5. Spring Boot 2.x基础教程:使用 ECharts 绘制各种华丽的数据图表

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 翟永超 来源 | http://blog.di ...

  6. OpenLayers基础教程——要素的编辑

    1.前言 在OpenLayers中,要素的编辑主要使用ol.interaction.Select和ol.interaction.Modify配合实现,下面开始介绍. 2.编辑要素 编辑功能的实现很简答 ...

  7. html5基础入门教程之canvas绘制图形

    moveTo(x,y) moveTo()方法用于定位绘画的位置,即将点移动到参数x,y所指定的坐标位置. canvas初始化或者调用了beginPath()方法时,绘画开始的位置即原点(0,0),使用 ...

  8. OpenLayers基础教程——要素样式的创建

    1.前言 在前面的博客中,ol.style这个类的出镜率很高,但限于篇幅的原因,所以一直没有进行详细介绍,下面就来介绍一下openlayers中的样式--ol.style. 2.样式的基本组成 一个o ...

  9. OpenLayers基础教程——popup弹出框

    1.前言 在OpenLayers中,一般使用ol.Overlay实现popup弹出框,弹出框一般用于显示地图上兴趣点的一些属性信息,如下图所示.下面开始介绍实现方法. 2.准备测试数据 在SqlSer ...

最新文章

  1. [codeforces]Round #538 (Div. 2) F. Please, another Queries on Array?
  2. 基于异或,取反和循环移位实现一个简单的加密解密函数
  3. 信息系统项目管理师论文怎么准备?49分论文备考经验
  4. 线程池:ThreadPoolExecutor
  5. am335x uboot, kernel 编译
  6. 2数据库表增加一个字段_14个实用的数据库设计技巧!
  7. Android之解析GML并显示
  8. 前端开发知识点解答-HTML-面试
  9. testng 定时构建_10自动化测试_持续集成篇
  10. PAT (Basic Level) Practice (中文)1041 考试座位号 (15 分)
  11. h5难做吗_H5系列课程| 做一只涨工资的H5 没有你想得那么难
  12. 谷歌浏览器导致电脑右下角莫名弹出广告解决办法
  13. selenium是python_selenium+Python(事件)
  14. 百度SiteApp网站打不开,手机站可以这样搞掂
  15. 2018宝鸡市高三数学第一次质量检测
  16. Python netCDF4
  17. html/css面试题(3)
  18. 都说ScreenToGif是GIF录制神器,却不知其强大之处远不在此
  19. XBL绑定组建的实例
  20. matplotlib sci论文画图技巧

热门文章

  1. 洛谷 P1046 陶陶摘苹果 C语言
  2. Android 时间转换 今天 昨天 前天 的样式
  3. 基于Cisco PacketTrancer的企业/校园双核心热备+WLC、AC无线控制器的无线网络拓扑规划
  4. jquery file upload 限制上传文件的格式、大小或图片尺寸
  5. 《IT经理世界》封面报道:淘宝隐忧3
  6. 安卓AES加解密(兼容Android7.0)
  7. 经典网页设计:20个新鲜出炉的 HTML5 网站
  8. 迅速提取网站URL链接-一键批量抓取网站链接
  9. mac音乐雷达:Shazam for Mac
  10. java argument是什么_第2期:argument、parameter以及option有什么区别?