作者跟随视频学习ol库的调用与功能实现,进行初步总结与回顾。
声明:参考新中地的文档,进行作者日后复习再次学习的简化。
GIS(Geographic Information System)是一种用于存储、分析、管理和展示地理数据的计算机系统。以下是 GIS 中的一些核心概念:
1、一张Map
是由很多Layer
图层组成的。
2、一个Layer
对应一个Source
矢量数据源
3、一个Source
由很多Feature
组成
4、Feature
是Geometry
和Style
组成
OpenLayers是一个开源的地图显示库,它是基于JavaScript语言实现的。下面是OpenLayers的核心概念:
"Source"是一个抽象的概念,它是用来获取图层数据的。OpenLayers支持多种不同类型的数据源,如:
每个数据源都有其特定的配置选项,例如瓦片数据源需要指定瓦片地址模板,而矢量数据源需要指定矢量数据的URL。
通过选择不同的数据源类型,并配置相应的参数,可以实现多种类型的图层显示效果。因此,"source"在OpenLayers中是一个很重要的概念,它决定了图层显示的内容。
这些概念是OpenLayers的核心概念,通过灵活的配置和扩展,可以实现复杂的地图显示效果。
Document
1、参数target:
制定初始化的地图设置到html
页面上的哪一个DOM元素上
2、参数layers:
通过这个参数的名字我可以推断一个map
可以设置多个layer
,图层是构成openlayers的基本单位,地图是由多个layer组成的,这种设计类似于Photoshop里面的图层,多个图层是可以叠加的,在最上面的会覆盖下面的。
3、参数view:
视图是设置地图的显示范围,包括中心点,放大级别,坐标
EPSG:4326 和 EPSG:3857 是两个常用的坐标参考系代码,用于在 GIS 中表示地理位置。它们的主要区别如下:
因此,两个坐标系的主要区别在于它们使用的坐标系统不同:EPSG:4326 使用的是经纬度,而 EPSG:3857 使用的是平面墨卡托投影。
EPSG:3857 在在线地图中被广泛使用,因为它能够在 Web 地图上提供更高的精度和更好的分辨率。然而,EPSG:4326 在网络上传输地理位置信息时被更多地使用,因为它使用的是标准的地理坐标系。
总的来说,选择使用哪个坐标系取决于你的应用需求:如果需要高精度和分辨率,选择 EPSG:3857;如果需要标准的地理坐标系,选择 EPSG:4326
/* 视图跳转控件 */
const ZoomToExtent = new ol.control.ZoomToExtent({
extent: [110, 30, 160, 30],
})
map.addControl(ZoomToExtent)
/* 放大缩小控件 */
const zoomslider = new ol.control.ZoomSlider();
map.addControl(zoomslider)
//全屏控件
const fullScreen = new ol.control.FullScreen();
map.addControl(fullScreen)
/* 1、构建要素 */
var point = new ol.Feature({
geometry: new ol.geom.Point([114.30, 30.50])
})
let style = new ol.style.Style({
image: new ol.style.Circle({
radius: 10,
fill: new ol.style.Fill({
color: "#ff2d51"
}),
stroke: new ol.style.Stroke({
color: "#333",
width: 2
})
})
})
point.setStyle(style);
/* 2、将要素添加到矢量数据源 */
const source = new ol.source.Vector({
features: [point]
})
/* 3、将矢量数据源添加到矢量图层 */
const layer = new ol.layer.Vector({
source
})
/* 4、将矢量图层添加到地图容器 */
map.addLayer(layer)
geojson
数据是矢量数据,是包含地理信息的json数据,格式是以key:value的形式存在的。后缀以geojson
结尾
var data = {
type: "FeatureCollection",
features: [
{
type: "Feature",
geometry: {
type: "Point",
coordinates: [114.30, 30.50]
}
}
]
}
var source = new ol.source.Vector({
/* 将geojson数据设置给实例数据源 */
features: new ol.format.GeoJSON().readFeatures(data)
})
var layer = new ol.layer.Vector({
source
})
map.addLayer(layer);
设置样式
Polygon
区var data = {
type: "FeatureCollection",
features: [
{
type: "Feature",
geometry: {
type: "Polygon",
coordinates: [[[114.30, 30.50], [116, 30.50], [116, 30]]]
}
}
]
}
//设置样式
const style = new ol.style.Style({
//边线颜色
stroke: new ol.style.Stroke({
color: '#ff2d51',
width: 2
}),
//设置填充色
fill: new ol.style.Fill({
color: "rgba(50, 50, 50, 0.3)"
})
})
geojson
文件的数据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)
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)
map.on("click",(evt)=>{
var position = evt.coordinate;
console.log(position)
})
//...初始化地图map
var source = new ol.source.Vector({
});
var layer = new ol.layer.Vector({
source
})
map.addLayer(layer);
map.on("click", (evt) => {
var position = evt.coordinate;
var point = new ol.Feature({
geometry: new ol.geom.Point(position)
})
source.addFeature(point)
})
核心API map.getView.animate()
map.on("click", (evt) => {
var position = evt.coordinate;
map.getView().animate({
center: position,
zoom: 10,
duration: 2000,
})
})