【07】openlayers 矢量图层
创建地图:
//创建地图
var map = new ol.Map({
//设置显示地图的视图
view: new ol.View({
center: [0, 0],//义地图显示中心于经度0度,纬度0度处
zoom: 1 //定义地图显示层级为1
}),
//创建地图图层
layers: [
//创建一个使用Open Street Map地图源的瓦片图层
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
//让id为map的div作为地图的容器
target: 'map'
});
创建矢量图层:
//矢量图层vectorlayer
let vectorlayer = new ol.layer.Vector({
source:source,//矢量图层源
style:style,//矢量图层样式
opacity:1,//透明度,默认为1
visible:true,//是不显示,默认true
extent:[100,34,103,36],//可选参数,图层渲染范围,[minLon,minLat,maxLon,maxLat]
zIndex:1,//图层渲染索引,默认是按加载顺序叠加
})
map.addLayer(vectorlayer)
矢量图层关于map的方法:
//添加矢量图层
map.addLayer(vectorlayer)
//删除切片图层
map.removeLayer(vectorlayer)
矢量图层自身方法:
//矢量图层的常用方法
//获取-设置,图层的可见范围
vectorlayer.getExtent();
vectorlayer.setExtent([100,34,103,36]);
//获取-设置,图层最大缩放级别
vectorlayer.getMaxZoom()
vectorlayer.setMaxZoom(18)
//获取-设置,图层最小缩放级别
vectorlayer.getMinZoom()
vectorlayer.setMinZoom(2)
//获取-设置,图层的不透明度
vectorlayer.getOpacity()
vectorlayer.setOpacity(1)
//获取-设置,图层源
vectorlayer.getSource()
vectorlayer.setSource(new ol.source.OSM())
//获取-设置,图层的可见性
vectorlayer.getVisible()
vectorlayer.setVisible(true)
//获取-设置,图层的Z索引
vectorlayer.getZIndex()
vectorlayer.setZIndex(2)
//绑定事件-取消事件 type事件类型,listener函数体
vectorlayer.on(type,listener)
vectorlayer.un(type,listener) //获取与视口上给定像素相交的最高要素
vectorlayer.getFeatures([200,600])
//获取-设置,图层样式
vectorlayer.getStyle()
vectorlayer.setStyle(style)
下面重点介绍:矢量图层样式(style) 和 矢量图层源(source)
矢量样式:style
let style = new ol.style.Style({
    //填充样式(面)
    fill: new ol.style.Fill({
        color: 'rgba(255, 255, 255, 0.2)'
    }),
    //描边样式(线)
    stroke: new ol.style.Stroke({
        color: '#ffcc33',
        width: 2
    }),
    //图像样式
    image:image,
    //文字样式
    text:new ol.style.Text({
        font:'20px sans-serif',//字体大小,样式
        offsetX:0,//水平文本偏移量(以像素为单位)
        offsetY:0,//垂直文本偏移量(以像素为单位)
        scale:1,//字体放大倍数
        rotation:(Math.PI/180)*30,//旋转角度(顺时针旋转)
        text:'哇哈哈',//文字内容
        textAlign:'center',// 文字对齐方式 'left','right','center'
        fill:new ol.style.Fill({//文字填充颜色
            color: 'green'
        }),
        stroke: new ol.style.Stroke({//描边样式
            color: '#fff',
            width: 2
        }),
        padding:[0,0,0,0],//在文本周围填充像素
    })
})
矢量样式里的图像样式:image
//图像样式(点)
let image = new ol.style.Circle({
radius: 10,
fill: new ol.style.Fill({
color: 'red'
}),
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2
})
})
//如果是图标
let image = new ol.style.Icon({
src:'',//图片来源
color:'',//图标颜色,
opacity:1,//透明度
scale:1,//放大缩小倍数
rotation:(Math.PI/180)*deg,//旋转角度,顺时针旋转
})
//如果是正方形
let image = new ol.style.RegularShape({
fill: new ol.style.Fill({
color: 'red'
}),//填充色
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2
}),//边线样式
points: 4,//边数
radius: 10,//半径
angle: (Math.PI/4),//形状的角度(弧度单位)
rotation:(Math.PI/180)*deg,//旋转角度
rotateWithView:false,//是否跟随视图旋转形状
})
//如果是五角星
let image = new ol.style.RegularShape({
fill: new ol.style.Fill({
color: 'red'
}),//填充色
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2
}),//边线样式
points: 5,//边数
radius: 10,//半径
radius2: 4,//内半径
angle: 0//形状的角度(弧度单位)
})
//如果是三角形
let image = new ol.style.RegularShape({
fill: new ol.style.Fill({
color: 'red'
}),//填充色
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2
}),//边线样式
points: 3,//边数
radius: 10,//半径
rotation: (Math.PI/180)*deg,//旋转角度
angle: 0//形状的角度(弧度单位)
})
//如果是十字
let image = new ol.style.RegularShape({
fill: new ol.style.Fill({
color: 'red'
}),//填充色
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2
}),//边线样式
points: 4,//边数
radius: 10,//半径
radius2: 0,//内半径
angle: 0//形状的角度(弧度单位)
})
//如果是X型
let image = new ol.style.RegularShape({
fill: new ol.style.Fill({
color: 'red'
}),//填充色
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2
}),//边线样式
points: 4,//边数
radius: 10,//半径
radius2: 0,//内半径
angle: (Math.PI/4)//形状的角度(弧度单位)
})
矢量样式style的方法:
//克隆
style.clone()
//获取
style.getFill()
style.getStroke();
style.getImage();
style.getText();
//设置
style.setFill(fill)
style.setImage(image)
style.setStroke(stroke)
style.setText(text)
矢量图层源:point,line,polygon,wkt
//矢量图层源 :point,line,polygon,wkt
let source = new ol.source.Vector({
features:[point,line,polygon,wkt]//矢量
})
//矢量点 - 图标
let point = new ol.Feature(
new ol.geom.Point([108,34])
)
//矢量 线
let line = new ol.Feature(
new ol.geom.LineString([[108,34],[100,34],[100,40]])
)
//矢量面
let polygon = new ol.Feature(
new ol.geom.Polygon([[[90,34],[108,34],[90,40]]])
)
//wkt
let wktData = 'POLYGON((10.689 -25.092, 34.595 ' +
'-20.170, 38.814 -35.639, 13.502 ' +
'-39.155, 10.689 -25.092))';
let wkt = (new ol.format.WKT()).readFeature(wktData, {
dataProjection: 'EPSG:4326',//数据的投影
featureProjection: 'EPSG:3857'//创建的要素几何的投影
})
矢量图层源:geojson
//矢量图层源:geojson
//加载方法一
let source = new ol.source.Vector({
url:'data.geojson',//geojson 路径
format: new ol.format.GeoJSON({
dataProjection:'EPSG:4326',//默认数据投影'EPSG:4326'
})
})
//加载方法二
let source = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(geojsonData)
})
矢量图层源:topjson
//矢量图层源:topjson
let source = new ol.source.Vector({
url: 'topjson.json',//路径
format: new ol.format.TopoJSON({
layers: ['countries'] //子级名称
}),
overlaps: false,//该源可能具有重叠的几何形状,是否重叠
})
矢量图层源:kml
//矢量图层源:kml
let source = new ol.source.Vector({
url: "data.kml",
format: new ol.format.KML({
extractStyles:true,//从KML中提取样式
showPointNames:true,//将名称显示为包含点的地标的标签
writeStyles:true,//将样式写入KML
})
})
矢量图层源:gpx
let source = new ol.source.Vector({
    // GPX数据路径
    url: 'data.gpx',
    // 指定数据格式为GPX
    format: new ol.format.GPX()
})
矢量图层源:mvt
let source = new ol.source.VectorTile({
    format: new ol.format.MVT(),
    url: '',//数据路径
})
矢量图层源 source 的自身方法:
//source 方法
//添加-删除feature
source.addFeature(feature)
source.removeFeature(feature)
source.addFeatures(features)
//遍历feature
source.forEachFeature(callback)
//获取
source.getFeatureById(id)
source.getFeatures()
//绑定事件-取消事件 type事件类型,listener函数体
source.on(type,listener)
source.un(type,listener)
矢量点聚合Cluster
//点聚合Cluster
let clusterSource = new ol.source.Cluster({
distance:20,//间隔最小距离,默认20像素
source: source,//资源,点
});
//设置距离
clusterSource.setDistance(10)
【07】openlayers 矢量图层的更多相关文章
- OpenLayers在多个矢量图层编辑要素
		
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...
 - openlayers3 在地图上叠加WFS查询矢量图层
		
随着终端设备计算能力的加强,用户在使用地图的时候也须要越来越多的交互效果. 比方如今非常火的室内导航,为了获得好的用户体验,就须要当用户单击某一商店的时候该商店的颜色能对应的变化.这就须要叠加矢量图层 ...
 - GeoServer中利用SLD配图之矢量图层配图
		
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1 背景 我们在ArcMap中可以直接通过symbol功能对图层进行定 ...
 - (十二) WebGIS中矢量图层的设计
		
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.前言 在前几章中我们已经了解了什么是矢量查询.屏幕坐标与地理坐标之 ...
 - (七)WebGIS中栅格、矢量图层设计之栅格、矢量图层的本质
		
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.何为栅格数据,何为矢量数据? 在GIS中,对于数据格式的分类,我们 ...
 - QGis(三)查询矢量图层的要素属性字段值(转载)
		
QGis(三)查询矢量图层的要素属性字段值 https://github.com/gwaldron/osgearth/issues/489 当加载一个矢量图层后,如果要查看要素的属性字段值,则需要实现 ...
 - ArcGIS统计栅格像元值并转换为矢量图层
		
很多时候,我们需要得到矢量数据区域所对应栅格数据的像元统计值(求平均.求和等),然后将获得的统计值赋给矢量图层的属性表,在ArcGIS中操作如下:(PS:第一次写技术文章,望大家多多体谅与支持,么么哒 ...
 - Qt+QGIS二次开发:向shp矢量图层中添加新的字段
		
添加一个新的字段到shp文件中,并且从Excel里导入数据到该字段.原shp文件里的字段ID应该与Excel里的字段ID一一对应才能正确的导入.下图分别是shp的字段和Excel的字段 将class字 ...
 - Qt+QGis二次开发:矢量图层的显示样式
		
原文链接:QGis二次开发基础 -- 矢量图层的显示样式
 
随机推荐
- 吴裕雄--天生自然 HADOOP大数据分布式处理:CenterOS 7 多台物理机、虚拟机相互桥连接ping通,并且能够成功连接外网
			
选择用于桥接模式下的虚拟交换机,并且要选择对应的有线或者无线的网卡,如果主机是插网线联网的,那就选择有线网卡,如果主机是连无线网络的就选择无线网卡.Realtek PCIe GBE Family Co ...
 - 线程同步Lock锁
			
Lock接口历史 java1.5版本之前只有synchronized一种锁,lock是java1.5版本之后提供的接口.lock接口与synchronized接口功能相同,但是需要手动获取锁和释放锁. ...
 - win10安装3DMAX失败,怎么强力卸载删除注册表并重新安装
			
一些搞设计的朋友在win10系统下安装3DMAX失败或提示已安装,也有时候想重新安装3DMAX的时候会出现本电脑windows系统已安装3DMAX,你要是不留意直接安装3DMAX,只会安装3DMAX的 ...
 - HttpClient-get请求/Post请求/Post-Json/Header
			
1.Pom文件添加httpClient 依赖 <dependency> <groupId>org.apache.httpcomponents</groupId> & ...
 - 吴裕雄--天生自然 R语言开发学习:基本图形
			
#---------------------------------------------------------------# # R in Action (2nd ed): Chapter 6 ...
 - node新人
			
node 使用 http和express创建服务器环境 如 apache iis等 不需要配置一堆文件 为啥使用node 省事 v8引擎 异步js 不影响浏览者浏览网站 redis ...
 - Servlet+JSP 对外访问路径配置
			
servlet类似 servlet配置为: <servlet> <servlet-name>Demo01_OutWrite</servlet-name> ...
 - Linux设置redis密码登录
			
第一种:永久方式 redis设置密码访问 你的redis在真是环境中不可以谁想访问就可以访问,所以必须要设置密码 设置密码的流程如下: vim /etc/redis.conf 找到 #require ...
 - shell知多少?
			
Shell字面理解就是个"壳",是操作系统(内核)与用户之间的桥梁,充当命令解释器的作用,将用户输入的命令翻译给系统执行.Linux中的shell与Windows下的DOS一样,提 ...
 - Eclipse无Server或者Tomcat8.5解决办法
			
原文链接:https://blog.csdn.net/fangzicheng/article/details/78333567 ...