【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二次开发基础 -- 矢量图层的显示样式
随机推荐
- 79)PHP,session函数编写的注意事项
(1)先执行 session_set_save_handler() 在session_start(). (2)那么开启session_start(),有两种方法,一个就是session_start ...
- ZOJ-4089-Little Sub and Isomorphism Sequences
给定你个数组,以及一些单点修改,以及询问,每次询问需要求得,最长的字串长度,它在其他位置存在同构. 当存在两个不相交的区间同构时,如: 1.2.…….n -1.n.n + 1.…….m.m + 1.m ...
- Java发送Post请求,参数JSON,接收JSON
/** * 发送post请求 * @param url 路径 * @param jsonObject 参数(json类型) * @param encoding 编码格式 * @return * @th ...
- 整合SSM遇到的错误,数据库连接失败问题集合
Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be a ...
- printf 输出浮点数
在测试printf函数输出结果时,写了如下代码: /** * printf:格式化输出函数 * printf函数不会按照格式控制而对数据类型进行转换,不管三七二十一, * 抓到二进制数据就按照格式控制 ...
- cisco WLC开启portal认证,但是访问https无法跳转问题的解决
config network web-auth https-redirect enable版本8,及以上才支持 官方文档: http://www.cisco.com/c/zh_cn/support ...
- linux下载文件到本地_把linux服务器的文件下到本地windows
tar -cvf script.tar scriptsz script.tar 文件夹先要打包,并且要指定打包的名字. 具体: sz/rz命令: 一般来说,linux服务器大多是通过ssh来进行远 ...
- AtomicBoolean介绍
网上资料: 使用 AtomicBoolean 高效并发处理 "只初始化一次" 的功能要求: 1 privatestatic AtomicBoolean initialized = ...
- python 内置方法、数据序列化
abc(*args, **kwargs) 取绝对值 def add(a,b,f): return f(a)+f(b) res = add(3,-6,abs) print(res) all(*args, ...
- javascript学习内容
http协议 犀牛书 MDN js单线程 let只在代码块内有效 es5只有全局作用域 const变量指向的内存地址不得改动,值不能保证不变 全局变量不加var node.js 更改连接到服务器的方式 ...