openlayers完全是以对象的形式调用的。
基本就是一层包一层。
new map({
layers: [new TileLayer({
sorces: new XYZ({
url: '......'
})
})]
target: "map",
view: new view({
center: [121.324, 34.45]
zoom: 10,
projection:"EPSG:4326",
})
})
我们为了更好的操作属性和方法。会将它扁平化。其实就是将内部的属性在外面先定义好。然后在内测套用即可。
const view = new view({})
const sorces = new XYZ({})
const layers = [new TileLayer({
sorces
})
]
new map({
layers,
sorces
})
其实在计算器内部堆栈中也是这样做的。
对象不是一层套一层。对象是扁平化的。通过指向另一个对象的地址来找到下一个对象的。
注意调用顺序。被调用的内容先写在上方。 注意图层有很多个、是个数组。
TileLayer 瓦片层
layer有三种:
TileLayer:瓦片图层 通常用来加载底图、性能很好。根据用户缩放等操作。加载对应的瓦片信息。比较常用的XYZ, OSM。XYZ比较常用。因为OSM是国外提供的。需要翻墙、网速不好。
imageLayer: 静态图片图层。
vectorLayer:矢量图层,不会失真。常用来添加地图标注、路径等。
用的最多的就是矢量图层。因为tileLayer图层通常做底图。所以往往只加一个。也会取网上提供的地图。
而矢量不会失真可操作的内容较多。
常见的就是切换 卫星地图、城市道路地图、 3D地图等
const gaodeSource = new XYZ({
url: "https://1231231dsfaf.com"
})
const satelliteSource = new XYZ({
url: "https://webst0{1-4}.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}.com"
})
const markerSource = new XYZ({
url: "https://webst0{1-4}.is.autonavi.com/appmaptile?style=8&x={x}&y={y}&z={z}.com"
})
const gaodeLayer = new TileLayer({
source: gaodeSource
})
const satelliteLayer = new TileLayer({
source: satelliteSource
})
const markerLayer = new TileLayer({
source: markerSource
})
const map = new Map({
target: "map",
view,
layers:[gaodeLayer, satelliteLayer, markerLayer ]
})
gaodeLayer.setZIndex(100)
satelliteLayer.setZIndex(50)
markerLayer.setZIndex(30)
map.removeLayer(gaodeLayer)
map.addLayer(gaodeLayer)
gaodeLayer.setVisible(false/true)
XYZ的含义。 X,Y横纵坐标轴。 Z代表缩放级别。zoom
const extent = [114.25, 225, 235, 421]
const staticImageLayer = new ImageLayer({
source: new Static({
url:"/peiqi.webp",
imageExtent: extent
})
})
import VectorLayer from 'ol/layer/Vector.js'
import VectorSource from 'ol/source/Vector.js'
const vectorLayer = new VectorLayer({
source: new VectorSource({
url: 'https://geo.datav.aliyun.com/areas_v3/bound/10000.json',
format: new GeoJSON()
})
style: new Style({
file: new File({
color: "red"
})
})
})
{
"type":'FeatureCollection',
features:[....]
要素集合单个item{
type:feature,
properties{
},
geometry{
type: "Mu"
coordinates:[] 经纬度信息
}
}
}
map.addLayer(vectorLayer)