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.
|
Fires:
drawend
(ol.DrawEvent) - Triggered upon feature draw end
drawstart
(ol.DrawEvent) - Triggered upon feature draw start
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 |
2、select
ol.interaction.Select
new ol.interaction.Select(opt_options)
Name | Type | Description | |||
---|---|---|---|---|---|
options | Options.
|
Extends
Methods
getFeatures(){ol.Collection.<ol.Feature>}
3、modify
ol.interaction.Modify
new ol.interaction.Modify(options)
Name | Type | Description | |||
---|---|---|---|---|---|
options | Options.
|
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 |
实现实例:
1、draw
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <head>
- <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
- <title>Ol3draw</title>
- <linkrel="stylesheet"type="text/css"href="http://localhost/ol3/css/ol.css"/>
- <styletype="text/css">
- body,#map{
- border:0px;
- margin:0px;
- padding:0px;
- 100%;
- height:100%;
- font-size:13px;
- }
- .form-inline{
- position:absolute;
- top:10pt;
- right:10pt;
- z-index:99;
- }
- </style>
- <scripttype="text/javascript"src="http://localhost/ol3/build/ol.js"></script>
- <scripttype="text/javascript"src="http://localhost/jquery/jquery-1.8.3.js"></script>
- <scripttype="text/javascript">
- functioninit(){
- varformat='image/png';
- varbounds=[73.4510046356223,18.1632471876417,
- 134.976797646506,53.5319431522236];
- varuntiled=newol.layer.Image({
- source:newol.source.ImageWMS({
- ratio:1,
- url:'http://localhost:8081/geoserver/lzugis/wms',
- params:{'FORMAT':format,
- 'VERSION':'1.1.1',
- LAYERS:'lzugis:province',
- STYLES:''
- }
- })
- });
- varprojection=newol.proj.Projection({
- code:'EPSG:4326',
- units:'degrees'
- });
- varmap=newol.Map({
- controls:ol.control.defaults({
- attribution:false
- }),
- target:'map',
- layers:[
- untiled
- ],
- view:newol.View({
- projection:projection
- })
- });
- map.getView().fitExtent(bounds,map.getSize());
- varsource=newol.source.Vector();
- varvector=newol.layer.Vector({
- source:source,
- style:newol.style.Style({
- fill:newol.style.Fill({
- color:'rgba(255,0,0,0.2)'
- }),
- stroke:newol.style.Stroke({
- color:'#ffcc33',
- 2
- }),
- image:newol.style.Circle({
- radius:7,
- fill:newol.style.Fill({
- color:'#ffcc33'
- })
- })
- })
- });
- map.addLayer(vector);
- vartypeSelect=document.getElementById('type');
- vardraw;//globalsowecanremoveitlater
- functionaddInteraction(){
- varvalue=typeSelect.value;
- if(value!=='None'){
- draw=newol.interaction.Draw({
- source:source,
- type:/**@type{ol.geom.GeometryType}*/(value)
- });
- map.addInteraction(draw);
- }
- }
- /**
- *Letuserchangethegeometrytype.
- *@param{Event}eChangeevent.
- */
- typeSelect.onchange=function(e){
- map.removeInteraction(draw);
- addInteraction();
- };
- addInteraction();
- }
- </script>
- </head>
- <bodyonLoad="init()">
- <divid="map">
- <formclass="form-inline">
- <label>选择绘制类型:</label>
- <selectid="type">
- <optionvalue="None">None</option>
- <optionvalue="Point">点</option>
- <optionvalue="LineString">线</option>
- <optionvalue="Polygon">多边形</option>
- </select>
- </form>
- </div>
- </body>
- </html>
2、select
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <head>
- <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
- <title>Ol3select</title>
- <linkrel="stylesheet"type="text/css"href="http://localhost/ol3/css/ol.css"/>
- <styletype="text/css">
- body,#map{
- border:0px;
- margin:0px;
- padding:0px;
- 100%;
- height:100%;
- font-size:13px;
- }
- .form-inline{
- position:absolute;
- top:10pt;
- right:10pt;
- z-index:99;
- }
- </style>
- <scripttype="text/javascript"src="http://localhost/ol3/build/ol.js"></script>
- <scripttype="text/javascript"src="http://localhost/jquery/jquery-1.8.3.js"></script>
- <scripttype="text/javascript">
- varpoint="POINT(103.58429749802736.119086450265)";
- varline="MULTILINESTRING((106.51911520618636.119086450265,108.96712780981136.5936423859273))";
- varpolygon="MULTIPOLYGON(((106.51911520618629.4789248520356,108.96712780981134.2761116373967,113.22668288693523.1830703234799)))";
- varwkts=[point,line,polygon];
- varwktformat=newol.format.WKT();
- functioninit(){
- varformat='image/png';
- varbounds=[73.4510046356223,18.1632471876417,
- 134.976797646506,53.5319431522236];
- varuntiled=newol.layer.Image({
- source:newol.source.ImageWMS({
- ratio:1,
- url:'http://localhost:8081/geoserver/lzugis/wms',
- params:{'FORMAT':format,
- 'VERSION':'1.1.1',
- LAYERS:'lzugis:province',
- STYLES:''
- }
- })
- });
- varprojection=newol.proj.Projection({
- code:'EPSG:4326',
- units:'degrees'
- });
- varfeatures=newArray();
- for(vari=0;i<wkts.length;i++){
- varfeature=wktformat.readFeature(wkts[i]);
- feature.getGeometry().transform('EPSG:4326','EPSG:4326');
- features.push(feature);
- }
- varvector=newol.layer.Vector({
- source:newol.source.Vector({
- features:features
- }),
- style:newol.style.Style({
- fill:newol.style.Fill({
- color:'rgba(255,0,0,0.2)'
- }),
- stroke:newol.style.Stroke({
- color:'#ffcc33',
- 2
- }),
- image:newol.style.Circle({
- radius:7,
- fill:newol.style.Fill({
- color:'#ffcc33'
- })
- })
- })
- });
- varmap=newol.Map({
- controls:ol.control.defaults({
- attribution:false
- }),
- target:'map',
- layers:[
- untiled,vector
- ],
- view:newol.View({
- projection:projection
- })
- });
- map.getView().fitExtent(bounds,map.getSize());
- //选择对象
- varselect=null;//reftocurrentlyselectedinteraction
- //selectinteractionworkingon"singleclick"
- varselectSingleClick=newol.interaction.Select();
- //selectinteractionworkingon"click"
- varselectClick=newol.interaction.Select({
- condition:ol.events.condition.click
- });
- //selectinteractionworkingon"mousemove"
- varselectMouseMove=newol.interaction.Select({
- condition:ol.events.condition.mouseMove
- });
- varselectElement=document.getElementById('selecttype');
- varchangeInteraction=function(){
- if(select!==null){
- map.removeInteraction(select);
- }
- varvalue=selectElement.value;
- if(value=='singleclick'){
- select=selectSingleClick;
- }elseif(value=='click'){
- select=selectClick;
- }elseif(value=='mousemove'){
- select=selectMouseMove;
- }else{
- select=null;
- }
- if(select!==null){
- map.addInteraction(select);
- }
- };
- /**
- *onchangecallbackontheselectelement.
- */
- selectElement.onchange=changeInteraction;
- changeInteraction();
- }
- </script>
- </head>
- <bodyonLoad="init()">
- <divid="map">
- <formclass="form-inline">
- <label>选择高亮方式:</label>
- <selectid="selecttype">
- <optionvalue="none"selected>None</option>
- <optionvalue="singleclick">单击</option>
- <optionvalue="click">点击</option>
- <optionvalue="mousemove">鼠标经过</option>
- </select>
- </form>
- </div>
- </body>
- </html>
3、modify
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <head>
- <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
- <title>Ol3modify</title>
- <linkrel="stylesheet"type="text/css"href="http://localhost/ol3/css/ol.css"/>
- <styletype="text/css">
- body,#map{
- border:0px;
- margin:0px;
- padding:0px;
- 100%;
- height:100%;
- font-size:13px;
- }
- </style>
- <scripttype="text/javascript"src="http://localhost/ol3/build/ol.js"></script>
- <scripttype="text/javascript"src="http://localhost/jquery/jquery-1.8.3.js"></script>
- <scripttype="text/javascript">
- varpoint="POINT(103.58429749802736.119086450265)";
- varline="MULTILINESTRING((106.51911520618636.119086450265,108.96712780981136.5936423859273))";
- varpolygon="MULTIPOLYGON(((106.51911520618629.4789248520356,108.96712780981134.2761116373967,113.22668288693523.1830703234799)))";
- varwkts=[point,line,polygon];
- varwktformat=newol.format.WKT();
- functioninit(){
- varformat='image/png';
- varbounds=[73.4510046356223,18.1632471876417,
- 134.976797646506,53.5319431522236];
- varuntiled=newol.layer.Image({
- source:newol.source.ImageWMS({
- ratio:1,
- url:'http://localhost:8081/geoserver/lzugis/wms',
- params:{'FORMAT':format,
- 'VERSION':'1.1.1',
- LAYERS:'lzugis:province',
- STYLES:''
- }
- })
- });
- varprojection=newol.proj.Projection({
- code:'EPSG:4326',
- units:'degrees'
- });
- varfeatures=newArray();
- for(vari=0;i<wkts.length;i++){
- varfeature=wktformat.readFeature(wkts[i]);
- feature.getGeometry().transform('EPSG:4326','EPSG:4326');
- features.push(feature);
- }
- varvector=newol.layer.Vector({
- source:newol.source.Vector({
- features:features
- }),
- style:newol.style.Style({
- fill:newol.style.Fill({
- color:'rgba(255,0,0,0.2)'
- }),
- stroke:newol.style.Stroke({
- color:'#ffcc33',
- 2
- }),
- image:newol.style.Circle({
- radius:7,
- fill:newol.style.Fill({
- color:'#ffcc33'
- })
- })
- })
- });
- varselect=newol.interaction.Select();
- varmodify=newol.interaction.Modify({
- features:select.getFeatures()
- });
- vector.on("afterfeaturemodified",function(evt){
- console.log(evt);
- });
- varmap=newol.Map({
- interactions:ol.interaction.defaults().extend([select,modify]),
- controls:ol.control.defaults({
- attribution:false
- }),
- target:'map',
- layers:[
- untiled,vector
- ],
- view:newol.View({
- projection:projection
- })
- });
- map.getView().fitExtent(bounds,map.getSize());
- }
- </script>
- </head>
- <bodyonLoad="init()">
- <divid="map">
- </div>
- </body>
- </html>