WebGIS开发教程:Openlayers常见功能实现

Openlayers的核心

Openlayers如何实现交互式绘制

  • 创建矢量图层和矢量数据源 - 创建画布

  • 创建画笔

  • 激活画笔

如何加载天地图,设置中心点和显示级别

javascript 复制代码
const TianDiMap_cva = new ol.layer.Tile({ 
title:"天地图矢量注记图层", 
source:new ol.source.XYZ({ 
url:'http://t0.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=' + '8a5c2b00e94b49659861e064c37f778d', 
wrapX:false 
}) 
}) 
const map = new ol.Map({ 
target:'map_container', 
layers:[TianDiMap_vec,TianDiMap_cva], view:new ol.View({ 
projection:'EPSG:4326', 
center:[0,0], 
zoom:1 
}) 
}) 

如何实现图层切换

javascript 复制代码
const layers = map.getLayers().getArray(); 
map.addLayer(); 
map.removeLayer(); 
map.getLayers().item(index).setVisible(boolean) //实现图层的显示和隐藏 

Openlayer如何查询要素

在本地定义geojson数据

javascript 复制代码
var data = { 
type: "FeatureCollection", 
features: [ 
{ 
type: "Feature", 
geometry: { 
type: "Point", coordinates: [114, 30] 
}, 
properties:{ 
id:1001, 
name:"武汉" 
} 
},{ 
type:"Feature", 
geometry:{ 
type:"LineString", coordinates:[ 
[114,30], 
[120,30] 
] 
} 
} 
] 
} 
const 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: new ol.style.Circle({ 
radius: 8, 
fill: new ol.style.Fill({ 
color: '#ffcc3367' 
}), 
stroke: new ol.style.Stroke({ color: '#333', 
width: 2 
})
}) 
}) 
var source = new ol.source.Vector({ 
/* 将geojson数据设置给实例数据源 */ 
features: new ol.format.GeoJSON().readFeatures(data) }) 
var layer = new ol.layer.Vector({ 
source, 
style 
}) 
map.addLayer(layer); 
给要素定义样式 
var data = { 
type: "FeatureCollection", 
features: [ 
{ 
type: "Feature", 
geometry: { 
type: "Point", 
coordinates: [114, 30] 
} 
} 
] 
} 
const 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: new ol.style.Circle({ 
radius: 8, 
fill: new ol.style.Fill({ 
color: '#ffcc3367' 
}), 
stroke: new ol.style.Stroke({ 
color: '#333', 
width: 2 
}) 
}) 
}) 
var features = new ol.format.GeoJSON().readFeatures(data) //++ features[0].setStyle(style)//++ 
var source = new ol.source.Vector({ 
/* 将geojson数据设置给实例数据源 */ 
features 
}) 
var layer = new ol.layer.Vector({ 
source 
}) 
map.addLayer(layer); 

加载geojson⽂件的数据

javascript 复制代码
{ 
"type": "FeatureCollection", 
"features": [ 
{ 
"type": "Feature", 
"geometry": { 
"type": "Point", 
"coordinates": [114, 30] } 
} 
] 
} 
const source = new ol.source.Vector({ url: './data/map.geojson', format: new ol.format.GeoJSON() 
}) 
const layer = new ol.layer.Vector({ source 
}) 
map.addLayer(layer) 
复制代码

⼀定要以服务的方式去加载,否则报错

加载网络数据

javascript 复制代码
const source = new ol.source.Vector({ 
url: 'https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=420100', format: new ol.format.GeoJSON() 
}) 
const layer = new ol.layer.Vector({ 
source 
}) 
map.addLayer(layer) 

openlayer添加图层到指定位置

javascript 复制代码
const layers = map.getLayers(); 
layers.insertAt(index,layerName); 

openlayer的坐标转换

javascript 复制代码
var gcs4490 = ol.proj.transform([605906.540647357,2723639.37418766], new ol .proj.Projection({code:'EPSG:4548'}),new ol.proj.Projection({code:'EPSG:449 0'})); 
相关推荐
ljf88383 分钟前
Java导出复杂excel,自定义excel导出
java·开发语言·excel
真*小白4 分钟前
Python语法学习篇(三)【py3】
开发语言·python·学习
前端小巷子5 分钟前
JS打造“九宫格抽奖”
前端·javascript·面试
潘小安7 分钟前
『译』资深前端开发者如何看待React架构
前端·react.js·面试
好望角雾眠15 分钟前
第四阶段C#通讯开发-1:通讯基础理论,串口,通讯模式,单位转换,代码示例
开发语言·笔记·c#·串口·通讯
不一样的故事12618 分钟前
学习Python是一个循序渐进的过程,结合系统学习、持续实践和项目驱动,
开发语言·python·学习
李昊哲小课32 分钟前
HTML 完整教程与实践
前端·html
eqwaak033 分钟前
科技信息差(9.13)
大数据·开发语言·人工智能·华为·语言模型
郝学胜-神的一滴37 分钟前
深入探索 Python 元组:从基础到高级应用
运维·服务器·开发语言·python·程序人生
一只乔哇噻41 分钟前
java后端工程师进修ing(研一版‖day44)
java·开发语言·学习·算法