ol 加载geoserver wms wmts mvt
- ol 加载geoserver wms
- 代码如下
-
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>geoserver-wms</title> <link href="https://cdn.bootcdn.net/ajax/libs/openlayers/4.6.5/ol-debug.css" rel="stylesheet"> <script src="https://cdn.bootcdn.net/ajax/libs/openlayers/4.6.5/ol-debug.js"></script> <style> html, body, #map { width: 100%; height: 100%; padding: 0; margin: 0; } </style> </head> <body> <div id="map"></div> </body> <script> var projection4326 = new ol.proj.Projection({ // code: 'EPSG:4490', code: 'EPSG:4326', units: 'degrees', }); var defaultView = new ol.View({ projection: projection4326, center: [113.3939, 22.977], //new ol.proj.fromLonLat([114.15, 22.65]), zoom: 10 }); //wms 服务 let testlyr = new ol.layer.Tile({ source: new ol.source.TileWMS({ url: "http://localhost:8880/geoserver/gwc/service/wms", params: { 'FORMAT': 'image/png', 'VERSION': '1.0.0', tiled: true, style: "", layers: 'postgis:ceshi0606', } }) }) //底图 let tileOSM = new ol.layer.Tile({ //source: new OSM() source: new ol.source.OSM() }); var map = new ol.Map({ controls: ol.control.defaults().extend([ new ol.control.ScaleLine({ units: 'degrees' }) ]), layers: [testlyr], target: 'map', view: defaultView }); </script> </html>
- ol 加载geoserver wmts
- 代码如下
-
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
<!
DOCTYPE
html>
<
html
>
<
head
>
<
meta
charset="UTF-8">
<
title
>geoserver-wmts</
title
>
<
link
href="https://cdn.bootcdn.net/ajax/libs/openlayers/4.6.5/ol-debug.css" rel="stylesheet">
<
script
src="https://cdn.bootcdn.net/ajax/libs/openlayers/4.6.5/ol-debug.js"></
script
>
<
style
>
html,
body,
#map {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
</
style
>
</
head
>
<
body
>
<
div
id="map"></
div
>
</
body
>
<
script
>
var projection4326 = new ol.proj.Projection({
// code: 'EPSG:4490',
code: 'EPSG:4326',
units: 'degrees',
extent: [-180, -90, 180, 90]
});
var projection4326 = ol.proj.get('EPSG:4326');
var defaultView = new ol.View({
projection: projection4326,
center: [113.3939, 22.977],
//new ol.proj.fromLonLat([114.15, 22.65]),
zoom: 10
});
//wmts 服务
const projectionExtent = projection4326.getExtent();
//console.log(ol.extent.getTopLeft(projectionExtent)); //[-180, 90]
const size = ol.extent.getWidth(projectionExtent) / 256;
// const resolutions = new Array(19);
const matrixIds = new Array(19);
for (let z = 0; z <
19
; ++z) {
// resolutions[z] = size / Math.pow(2, z);
matrixIds[z] = "EPSG:4326:" + z;
}
let resolutions = [0.703125, 0.3515625, 0.17578125, 0.087890625, 0.0439453125,
0.02197265625, 0.010986328125, 0.0054931640625, 0.00274658203125, 0.001373291015625,
6.866455078125E-4, 3.4332275390625E-4, 1.71661376953125E-4, 8.58306884765625E-5,
4.291534423828125E-5, 2.1457672119140625E-5, 1.0728836059570312E-5, 5.364418029785156E-6,
2.682209014892578E-6, 1.341104507446289E-6, 6.705522537231445E-7, 3.3527612686157227E-7];
var wmtsSource = new ol.source.WMTS({
url: 'http://localhost:8880/geoserver/gwc/service/wmts',
layer: "postgis:ceshi0606",
matrixSet: 'EPSG:4326',
format: 'image/png', // 默认:'image/jpeg'
projection: projection4326,
tileGrid: new ol.tilegrid.WMTS({
origin: ol.extent.getTopLeft(projectionExtent),
resolutions: resolutions,
matrixIds: matrixIds,
}),
style: '',
wrapX: true,
// crossOrigin: 'anonymous', //解决跨域问题,不好使请走nginx
// serverType: 'GEOSERVER', // wms 平台来源类型
// tileLoadFunction: function (imageTile, src) { //加header头请求
// fetch(src, {
// method: 'get',
// headers: { "szvsud-license-key": window.localStorage.baseMapLicenseKey?window.localStorage.baseMapLicenseKey:"b2OW+ghUXAZMQoUKUh49zT4+hmSdjlL65uCcmb+EFXbx/HntOygBZIN75AiGAGVi" },
// responseType: 'blob'
// }).then(res => {
// return res.blob()
// }).then(blob => {
// imageTile.getImage().src = URL.createObjectURL(blob);
// })
// }
});
var wmtsService = new ol.layer.Tile({
source: wmtsSource
});
var map = new ol.Map({
controls: ol.control.defaults().extend([
new ol.control.ScaleLine({
units: 'degrees'
})
]),
layers: [wmtsService],
target: 'map',
view: defaultView
});
</
script
>
</
html
>
- ol 加载geoserver mvt
- 代码如下
-
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>geoserver-mvt</title> <link href="https://cdn.bootcdn.net/ajax/libs/openlayers/4.6.5/ol-debug.css" rel="stylesheet"> <script src="https://cdn.bootcdn.net/ajax/libs/openlayers/4.6.5/ol-debug.js"></script> <style> html, body, #map { width: 100%; height: 100%; padding: 0; margin: 0; } </style> </head> <body> <div id="map"></div> </body> <script> var projection4326 = new ol.proj.Projection({ // code: 'EPSG:4490', code: 'EPSG:4326', units: 'degrees', extent: [-180, -90, 180, 90] }); var projection4326 = ol.proj.get('EPSG:4326'); var defaultView = new ol.View({ projection: projection4326, center: [113.3939, 22.977], //new ol.proj.fromLonLat([114.15, 22.65]), zoom: 10 }); //mvt 服务 const projectionExtent = projection4326.getExtent(); //console.log(ol.extent.getTopLeft(projectionExtent)); //[-180, 90] const size = ol.extent.getWidth(projectionExtent) / 256; // const resolutions = new Array(19); const matrixIds = new Array(19); for (let z = 0; z < 19; ++z) { // resolutions[z] = size / Math.pow(2, z); matrixIds[z] = "EPSG:4326:" + z; } let resolutions = [0.703125, 0.3515625, 0.17578125, 0.087890625, 0.0439453125, 0.02197265625, 0.010986328125, 0.0054931640625, 0.00274658203125, 0.001373291015625, 6.866455078125E-4, 3.4332275390625E-4, 1.71661376953125E-4, 8.58306884765625E-5, 4.291534423828125E-5, 2.1457672119140625E-5, 1.0728836059570312E-5, 5.364418029785156E-6, 2.682209014892578E-6, 1.341104507446289E-6, 6.705522537231445E-7, 3.3527612686157227E-7 ]; //加载geoserver mvt var gridsetName = 'EPSG:4326' var params = { 'REQUEST': 'GetTile', 'SERVICE': 'WMTS', 'VERSION': '1.0.0', 'LAYER': 'postgis:ceshi0606', 'STYLE': '', 'TILEMATRIX': gridsetName + ':{z}', 'TILEMATRIXSET': gridsetName, 'FORMAT': 'application/vnd.mapbox-vector-tile', 'TILECOL': '{x}', 'TILEROW': '{y}' } function urlConstruct() { let url = "http://localhost:8880/geoserver/gwc/service/wmts" + '?'; for (let param in this.params) { url = url + param + '=' + this.params[param] + '&'; } url = url.slice(0, -1); return url; } //mvt var mvtvectorLayer = new ol.layer.VectorTile({ source: new ol.source.VectorTile({ format: new ol.format.MVT(), projection: projection4326, tilePixelRatio: 1, tileGrid: new ol.tilegrid.WMTS({ origin: ol.extent.getTopLeft(projectionExtent), resolutions: resolutions, matrixIds: matrixIds, }), url:urlConstruct(), //url: "http://localhost:8880/geoserver/gwc/service/wmts?REQUEST=GetTile&SERVICE=WMTS&VERSION=1.0.0&LAYER=postgis:ceshi0606&STYLE=&FORMAT=application/vnd.mapbox-vector-tile&TILEMATRIX=EPSG:4326:{z}&TILEMATRIXSET=EPSG:4326&TILECOL={x}&TILEROW={y}" }) }) var map = new ol.Map({ controls: ol.control.defaults().extend([ new ol.control.ScaleLine({ units: 'degrees' }) ]), layers: [mvtvectorLayer], target: 'map', view: defaultView }); </script> </html>
分类:
openlayer
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律