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(16) #3 ——搜索与动态规划

    题目在这里啊 A.最长上升子序列,范围很小所以写了简单的O(n^2)算法 #include <iostream> #define rep(i, j, k) for(int i = j;i ...

  2. 关于单CPU,多CPU上的原子操作

    所谓原子操作,就是"不可中断的一个或一系列操作" . 硬件级的原子操作:在单处理器系统(UniProcessor)中,能够在单条指令中完成的操作都可以认为是" 原子操作& ...

  3. oracle数据库审计

    Oracle使用大量不同的审计方法来监控使用何种权限,以及访问哪些对象.审计不会防止使用这些权限,但可以提供有用的信息,用于揭示权限的滥用和误用. 下表中总结了Oracle数据库中不同类型的审计. 审 ...

  4. POJ - 3541 - Given a string…

    Given a string… Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 1819   Accepted: 390 C ...

  5. [Cogs728] [网络流24题#3] 最小路径覆盖 [网络流,最大流,二分图匹配]

    建图:源点—>边的起点(集合1中的)—>边的终点(集合2中的)—>汇点,所有边权均为1, 计算最大流,最后枚举起点的出边,边权为0的即为匹配上的, 可以这样理解:每条边表示起点和终点 ...

  6. 导出excel - 自用

    export function handerFillZero(num){ return num>=10 ? num : '0'+num; } export function exportExce ...

  7. ELECTRON新增模块的方法

    因为electron和node.js用的V8版本不一致,所以直接使用npm安装的模块可能在electron中不可用,特别是使用c.c++开发的模块.官方的说明:https://github.com/e ...

  8. [vagrant]第一次安装添加box出现问题汇总

    1.本地文件要加全文件名和协议file:/// 2.The box failed to unpackage properly. Please verify that the box file you' ...

  9. ioctl在socket中的一些用法及示例

    原文: http://blog.chinaunix.net/uid-20692625-id-3172833.html ----------------------------------------- ...

  10. javascript 获取当前对象

    <a href="dsfjlsdjf" onclick="testGet()"> 请教编写testGet()函数获取这个超链接href属性,限制例如 ...