zoukankan      html  css  js  c++  java
  • Openlays 3 绘制基本图形

    <body>
    <div id="menu">
    <label>几何图形类型:</label>
    <select id="type">
    <option value="None">无</option>
    <option value="Point">点</option>
    <option value="LineString">线</option>
    <option value="Polygon">多边形</option>
    <option value="Circle">圆</option>
    <option value="Square">正方形</option>
    <option value="Box">长方形</option>
    </select>
    </div>
    <div id="map"></div>
    <script>
    var map=new ol.Map({
    target:'map',
    layer:[],
    view:new ol.View({
    center:[0,0],
    zoom:2
    })
    });//初始化地图
    var OSM=new ol.layer.Tile({
    source:new ol.source.OSM()
    });
    map.addLayer(OSM);//添加地图数据
    var selectedType=document.getElementById("type");
    var draw;//绘制对象
    //绘制绘制层
    var source=new ol.source.Vector({wrapX:false});
    var vector=new ol.layer.Vector({
    source:source,
    style:new ol.style.Style({
    fill:new ol.style.Fill({
    color:'rgba(255,255,255,.2)'
    }),
    stroke:({
    stroke:new ol.style.Stroke({
    color:'#ffcc33',
    2
    })
    }),
    image:new ol.style.Circle({
    radius:7,
    fill:new ol.style.Fill({
    color:'#ffcc33'
    })
    })
    })
    });
    map.addLayer(vector);
    selectedType.onchange=function(e){
    map.removeInteraction(draw);//移除绘制图形
    addInteraction();
    };
    addInteraction();//添加交互绘制功能控件
    function addInteraction(){
    var value=selectedType.value;
    if(value!=="None"){
    if(source==null){
    source=new ol.source.Vector({
    wrapX:false
    });
    vector.setSource(source);
    }
    var geometryFunction,maxPoints;
    if(value==="Square"){
    value='Circle';
    geometryFunction=ol.interaction.Draw.createRegularPolygon(4);
    }else if(value==="Box"){
    value='LineString';
    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;
    };
    }
    draw= new ol.interaction.Draw({
    source:source,
    type:/**@type {ol,geom.GeometryType}*/(value),
    geometryFunction:geometryFunction,
    maxPoints:maxPoints
    });
    map.addInteraction(draw);
    }else{
    source=null;
    vector.setSource(source);
    }
    }
    </script>
    </body>

    
    
  • 相关阅读:
    Repeater1绑定数据,编辑数据的一些参考文章
    UrlRewritingNet 完美实现 ASP.NET 2.0 中的URL重写(映射) (转)
    asp.net下linkbutton的前后台使用方法
    sqlserver 中判断是否数字,是否汉字的方法
    asp.net中Excel导入(使用微软OLEDB驱动)
    asp.net获取URL和IP地址
    警告: 程序集绑定日志记录被关闭。
    301永久重定向asp.net实现方法
    Net程序如何防止被注入
    C++实现wc.exe程序
  • 原文地址:https://www.cnblogs.com/mina-huojian66/p/6019271.html
Copyright © 2011-2022 走看看