http://blog.csdn.net/gisshixisheng/article/details/46808647

概述:

本节主要讲述OL3的交互操作interaction,重点介绍draw,select以及modify。

接口说明:

OL3的interaction继承自ol.interaction.defaults,下面实现了以下几中交互操作:

创建方式为:

var interaction = new ol.interaction.InteractionType({options});
添加和移除方式为:
map.addInteraction(draw);
map.removeInteraction(draw);
1、draw
ol.interaction.Draw
new ol.interaction.Draw(options)

Name Type Description
options

Options.

Name Type Description

Fires:

Extends

Methods
on(type, listener, opt_this){goog.events.Key} inherited

Name Type Description
type string | Array.<string>

The event type or array of event types.

listener function

The listener function.

this Object

The object to use as this in listener.

2、select
ol.interaction.Select
new ol.interaction.Select(opt_options)

Name Type Description
options

Options.

Name Type Description

Extends

Methods
getFeatures(){ol.Collection.<ol.Feature>}
3、modify
ol.interaction.Modify
new ol.interaction.Modify(options)

Name Type Description
options

Options.

Name Type Description

Extends

Methods
on(type, listener, opt_this){goog.events.Key} inherited

Name Type Description
type string | Array.<string>

The event type or array of event types.

listener function

The listener function.

this Object

The object to use as this in listener.

实现实例:

1、draw

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Ol3 draw</title>
  5. <link rel="stylesheet" type="text/css" href="http://localhost/ol3/css/ol.css"/>
  6. <style type="text/css">
  7. body, #map {
  8. border: 0px;
  9. margin: 0px;
  10. padding: 0px;
  11. width: 100%;
  12. height: 100%;
  13. font-size: 13px;
  14. }
  15. .form-inline{
  16. position: absolute;
  17. top: 10pt;
  18. right: 10pt;
  19. z-index: 99;
  20. }
  21. </style>
  22. <script type="text/javascript" src="http://localhost/ol3/build/ol.js"></script>
  23. <script type="text/javascript" src="http://localhost/jquery/jquery-1.8.3.js"></script>
  24. <script type="text/javascript">
  25. function init(){
  26. var format = 'image/png';
  27. var bounds = [73.4510046356223, 18.1632471876417,
  28. 134.976797646506, 53.5319431522236];
  29. var untiled = new ol.layer.Image({
  30. source: new ol.source.ImageWMS({
  31. ratio: 1,
  32. url: 'http://localhost:8081/geoserver/lzugis/wms',
  33. params: {'FORMAT': format,
  34. 'VERSION': '1.1.1',
  35. LAYERS: 'lzugis:province',
  36. STYLES: ''
  37. }
  38. })
  39. });
  40. var projection = new ol.proj.Projection({
  41. code: 'EPSG:4326',
  42. units: 'degrees'
  43. });
  44. var map = new ol.Map({
  45. controls: ol.control.defaults({
  46. attribution: false
  47. }),
  48. target: 'map',
  49. layers: [
  50. untiled
  51. ],
  52. view: new ol.View({
  53. projection: projection
  54. })
  55. });
  56. map.getView().fitExtent(bounds, map.getSize());
  57. var source = new ol.source.Vector();
  58. var vector = new ol.layer.Vector({
  59. source: source,
  60. style: new ol.style.Style({
  61. fill: new ol.style.Fill({
  62. color: 'rgba(255, 0, 0, 0.2)'
  63. }),
  64. stroke: new ol.style.Stroke({
  65. color: '#ffcc33',
  66. width: 2
  67. }),
  68. image: new ol.style.Circle({
  69. radius: 7,
  70. fill: new ol.style.Fill({
  71. color: '#ffcc33'
  72. })
  73. })
  74. })
  75. });
  76. map.addLayer(vector);
  77. var typeSelect = document.getElementById('type');
  78. var draw; // global so we can remove it later
  79. function addInteraction() {
  80. var value = typeSelect.value;
  81. if (value !== 'None') {
  82. draw = new ol.interaction.Draw({
  83. source: source,
  84. type: /** @type {ol.geom.GeometryType} */ (value)
  85. });
  86. map.addInteraction(draw);
  87. }
  88. }
  89. /**
  90. * Let user change the geometry type.
  91. * @param {Event} e Change event.
  92. */
  93. typeSelect.onchange = function(e) {
  94. map.removeInteraction(draw);
  95. addInteraction();
  96. };
  97. addInteraction();
  98. }
  99. </script>
  100. </head>
  101. <body onLoad="init()">
  102. <div id="map">
  103. <form class="form-inline">
  104. <label>选择绘制类型:</label>
  105. <select id="type">
  106. <option value="None">None</option>
  107. <option value="Point">点</option>
  108. <option value="LineString">线</option>
  109. <option value="Polygon">多边形</option>
  110. </select>
  111. </form>
  112. </div>
  113. </body>
  114. </html>

2、select

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Ol3 select</title>
  5. <link rel="stylesheet" type="text/css" href="http://localhost/ol3/css/ol.css"/>
  6. <style type="text/css">
  7. body, #map {
  8. border: 0px;
  9. margin: 0px;
  10. padding: 0px;
  11. width: 100%;
  12. height: 100%;
  13. font-size: 13px;
  14. }
  15. .form-inline{
  16. position: absolute;
  17. top: 10pt;
  18. right: 10pt;
  19. z-index: 99;
  20. }
  21. </style>
  22. <script type="text/javascript" src="http://localhost/ol3/build/ol.js"></script>
  23. <script type="text/javascript" src="http://localhost/jquery/jquery-1.8.3.js"></script>
  24. <script type="text/javascript">
  25. var point = "POINT(103.584297498027 36.119086450265)";
  26. var line = "MULTILINESTRING((106.519115206186 36.119086450265,108.967127809811 36.5936423859273))";
  27. var polygon = "MULTIPOLYGON(((106.519115206186 29.4789248520356,108.967127809811 34.2761116373967,113.226682886935 23.1830703234799)))";
  28. var wkts = [point, line, polygon];
  29. var wktformat = new ol.format.WKT();
  30. function init(){
  31. var format = 'image/png';
  32. var bounds = [73.4510046356223, 18.1632471876417,
  33. 134.976797646506, 53.5319431522236];
  34. var untiled = new ol.layer.Image({
  35. source: new ol.source.ImageWMS({
  36. ratio: 1,
  37. url: 'http://localhost:8081/geoserver/lzugis/wms',
  38. params: {'FORMAT': format,
  39. 'VERSION': '1.1.1',
  40. LAYERS: 'lzugis:province',
  41. STYLES: ''
  42. }
  43. })
  44. });
  45. var projection = new ol.proj.Projection({
  46. code: 'EPSG:4326',
  47. units: 'degrees'
  48. });
  49. var features = new Array();
  50. for(var i=0;i<wkts.length;i++){
  51. var feature = wktformat.readFeature(wkts[i]);
  52. feature.getGeometry().transform('EPSG:4326', 'EPSG:4326');
  53. features.push(feature);
  54. }
  55. var vector = new ol.layer.Vector({
  56. source: new ol.source.Vector({
  57. features: features
  58. }),
  59. style: new ol.style.Style({
  60. fill: new ol.style.Fill({
  61. color: 'rgba(255, 0, 0, 0.2)'
  62. }),
  63. stroke: new ol.style.Stroke({
  64. color: '#ffcc33',
  65. width: 2
  66. }),
  67. image: new ol.style.Circle({
  68. radius: 7,
  69. fill: new ol.style.Fill({
  70. color: '#ffcc33'
  71. })
  72. })
  73. })
  74. });
  75. var map = new ol.Map({
  76. controls: ol.control.defaults({
  77. attribution: false
  78. }),
  79. target: 'map',
  80. layers: [
  81. untiled, vector
  82. ],
  83. view: new ol.View({
  84. projection: projection
  85. })
  86. });
  87. map.getView().fitExtent(bounds, map.getSize());
  88. //选择对象
  89. var select = null;  // ref to currently selected interaction
  90. // select interaction working on "singleclick"
  91. var selectSingleClick = new ol.interaction.Select();
  92. // select interaction working on "click"
  93. var selectClick = new ol.interaction.Select({
  94. condition: ol.events.condition.click
  95. });
  96. // select interaction working on "mousemove"
  97. var selectMouseMove = new ol.interaction.Select({
  98. condition: ol.events.condition.mouseMove
  99. });
  100. var selectElement = document.getElementById('selecttype');
  101. var changeInteraction = function() {
  102. if (select !== null) {
  103. map.removeInteraction(select);
  104. }
  105. var value = selectElement.value;
  106. if (value == 'singleclick') {
  107. select = selectSingleClick;
  108. } else if (value == 'click') {
  109. select = selectClick;
  110. } else if (value == 'mousemove') {
  111. select = selectMouseMove;
  112. } else {
  113. select = null;
  114. }
  115. if (select !== null) {
  116. map.addInteraction(select);
  117. }
  118. };
  119. /**
  120. * onchange callback on the select element.
  121. */
  122. selectElement.onchange = changeInteraction;
  123. changeInteraction();
  124. }
  125. </script>
  126. </head>
  127. <body onLoad="init()">
  128. <div id="map">
  129. <form class="form-inline">
  130. <label>选择高亮方式:</label>
  131. <select id="selecttype">
  132. <option value="none" selected>None</option>
  133. <option value="singleclick">单击</option>
  134. <option value="click">点击</option>
  135. <option value="mousemove">鼠标经过</option>
  136. </select>
  137. </form>
  138. </div>
  139. </body>
  140. </html>

3、modify

    1. <html xmlns="http://www.w3.org/1999/xhtml">
    2. <head>
    3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    4. <title>Ol3 modify</title>
    5. <link rel="stylesheet" type="text/css" href="http://localhost/ol3/css/ol.css"/>
    6. <style type="text/css">
    7. body, #map {
    8. border: 0px;
    9. margin: 0px;
    10. padding: 0px;
    11. width: 100%;
    12. height: 100%;
    13. font-size: 13px;
    14. }
    15. </style>
    16. <script type="text/javascript" src="http://localhost/ol3/build/ol.js"></script>
    17. <script type="text/javascript" src="http://localhost/jquery/jquery-1.8.3.js"></script>
    18. <script type="text/javascript">
    19. var point = "POINT(103.584297498027 36.119086450265)";
    20. var line = "MULTILINESTRING((106.519115206186 36.119086450265,108.967127809811 36.5936423859273))";
    21. var polygon = "MULTIPOLYGON(((106.519115206186 29.4789248520356,108.967127809811 34.2761116373967,113.226682886935 23.1830703234799)))";
    22. var wkts = [point, line, polygon];
    23. var wktformat = new ol.format.WKT();
    24. function init(){
    25. var format = 'image/png';
    26. var bounds = [73.4510046356223, 18.1632471876417,
    27. 134.976797646506, 53.5319431522236];
    28. var untiled = new ol.layer.Image({
    29. source: new ol.source.ImageWMS({
    30. ratio: 1,
    31. url: 'http://localhost:8081/geoserver/lzugis/wms',
    32. params: {'FORMAT': format,
    33. 'VERSION': '1.1.1',
    34. LAYERS: 'lzugis:province',
    35. STYLES: ''
    36. }
    37. })
    38. });
    39. var projection = new ol.proj.Projection({
    40. code: 'EPSG:4326',
    41. units: 'degrees'
    42. });
    43. var features = new Array();
    44. for(var i=0;i<wkts.length;i++){
    45. var feature = wktformat.readFeature(wkts[i]);
    46. feature.getGeometry().transform('EPSG:4326', 'EPSG:4326');
    47. features.push(feature);
    48. }
    49. var vector = new ol.layer.Vector({
    50. source: new ol.source.Vector({
    51. features: features
    52. }),
    53. style: new ol.style.Style({
    54. fill: new ol.style.Fill({
    55. color: 'rgba(255, 0, 0, 0.2)'
    56. }),
    57. stroke: new ol.style.Stroke({
    58. color: '#ffcc33',
    59. width: 2
    60. }),
    61. image: new ol.style.Circle({
    62. radius: 7,
    63. fill: new ol.style.Fill({
    64. color: '#ffcc33'
    65. })
    66. })
    67. })
    68. });
    69. var select = new ol.interaction.Select();
    70. var modify = new ol.interaction.Modify({
    71. features: select.getFeatures()
    72. });
    73. vector.on("afterfeaturemodified",function(evt){
    74. console.log(evt);
    75. });
    76. var map = new ol.Map({
    77. interactions: ol.interaction.defaults().extend([select, modify]),
    78. controls: ol.control.defaults({
    79. attribution: false
    80. }),
    81. target: 'map',
    82. layers: [
    83. untiled, vector
    84. ],
    85. view: new ol.View({
    86. projection: projection
    87. })
    88. });
    89. map.getView().fitExtent(bounds, map.getSize());
    90. }
    91. </script>
    92. </head>
    93. <body onLoad="init()">
    94. <div id="map">
    95. </div>
    96. </body>
    97. </html>

(转)OpenLayers3基础教程——OL3 介绍interaction的更多相关文章

  1. OpenLayers3基础教程——OL3 介绍control

    概述: 本文讲述的是Ol3中的control的介绍和应用. OL2和OL3 control比較: 相比較Ol2的control,OL3显得特别少,下图分别为Ol2和Ol3的control: Ol2的c ...

  2. (转) OpenLayers3基础教程——OL3 介绍control

    http://blog.csdn.net/gisshixisheng/article/details/46761535 概述: 本文讲述的是Ol3中的control的介绍和应用. OL2和OL3 co ...

  3. OpenLayers3基础教程——OL3之Popup

    概述: 本节重点讲述OpenLayers3中Popup的调用时实现,OL3改用Overlay取代OL2的Popup功能. 接口简单介绍: overlay跟ol.control.Control一样,是一 ...

  4. (转)OpenLayers3基础教程——OL3基本概念

    http://blog.csdn.net/gisshixisheng/article/details/46756275 OpenLayers3基础教程——OL3基本概念 从本节开始,我会陆陆续续的更新 ...

  5. OpenLayers3基础教程——OL3基本概念

    从本节開始,我会陆陆续续的更新有关OL3的相关文章--OpenLayers3基础教程,欢迎大家关注我的博客,同一时候也希望我的博客可以给大家带来一点帮助. 概述: OpenLayers 3对OpenL ...

  6. (转)OpenLayers3基础教程——OL3之Popup

    http://blog.csdn.net/gisshixisheng/article/details/46794813 概述: 本节重点讲述OpenLayers3中Popup的调用时实现,OL3改用O ...

  7. ActiveMQ基础教程----简单介绍与基础使用

    概述 ActiveMQ是由Apache出品的,一款最流行的,能力强劲的开源消息总线.ActiveMQ是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,它非常快速,支持多 ...

  8. Embedded Linux Primer----嵌入式Linux基础教程--章节介绍

    章节介绍 第一章,“导引”,简要介绍了Linux被迅速应用在嵌入式环境的驱动因素,介绍了与嵌入式Linux相关的几个重要的标准和组织. 第二章,“第一个嵌入式经历”,介绍了与后几章所构建的嵌入式Lin ...

  9. (转) OpenLayers3基础教程——加载资源

    概述: 本节讲述如何在Ol3中加载wms图层并显示到地图中. Ol3下载: 你可以在OL官网去下载,下载地址为http://openlayers.org/download/,也可以去我的百度云盘下载, ...

随机推荐

  1. BUPT2017 springtraining(15) #3

    这里这里 A.签到题 #include <cstdio> double a[] = {0.4, 0.16, 0.063, 0.025, 0.010, 0.004}; int main() ...

  2. qwb VS 去污棒

    qwb VS 去污棒 Time Limit: 2 Sec  Memory Limit: 256 MB Description qwb表白学姐失败后,郁郁寡欢,整天坐在太阳底下赏月.在外人看来,他每天自 ...

  3. Linux Container测试之block IO

      简介 Linux Container是OS级别的虚拟化方案,它相比于一般的虚拟机没有了硬件模拟以及指令模拟,相比传统虚拟机具有更低的开销,因此可以应用到私有云之中.LXC目前的版本支持对memor ...

  4. 2015 编程之美初赛第一场 AC题

    题目1 : 彩色的树 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵n个节点的树,节点编号为1, 2, …, n.树中有n - 1条边,任意两个节点间恰好有一条路 ...

  5. 【Java并发编程实战】—– AQS(四):CLH同步队列

    在[Java并发编程实战]-–"J.U.C":CLH队列锁提过,AQS里面的CLH队列是CLH同步锁的一种变形. 其主要从双方面进行了改造:节点的结构与节点等待机制.在结构上引入了 ...

  6. 抽象类(Abstract)和接口的不同点、共同点(Interface)。

    同样点: (1) 都能够被继承 (2) 都不能被实例化 (3) 都能够包括方法声明 (4) 派生类必须实现未实现的方法 区 别: (1) 抽象基类能够定义字段.属性.方法实现.接口仅仅能定义属性.索引 ...

  7. Extjs 3 控制Grid某行某列不可编辑

    var cmGoodsFee = new Ext.grid.ColumnModel([rmGoodsFee, { header : "id", tooltip : "id ...

  8. FreeBSD内核之中的一个 ALQ机制的使用

    背景: 笔者由于一个项目,这段时间在使用FreeBSD进行内核模块的编程. 之前做过一段时间的Linux下驱动模块编程.对Linux下的模块编程还算熟悉. 如今突然转到FreeBSD下.尽管Linux ...

  9. Android 6.0 中TimePicker显示为滚动样式的方法

    在Android6.0中,TimePicker控件的默认样式为转盘的样式,就像这个样子: 如果想要显示为之前的滚动样式的话也很简单,只要在布局文件中设置TimePicker的timePickerMod ...

  10. bzoj2503 相框——思路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2503 思路题: 首先,这种问题应该注意到答案只跟度数有关,跟其他什么连接方法之类的完全无关: ...