注意: 首先要破解mapbox才能加载本地化或者你们测试弄个token来也行
注意:EPSG:900913相当于3857坐标系 因为他们参数一致,mapbox目前只支持geoserver中的900913与3857坐标系
1.mapbox加载geoserver的wms格式
1.1mapbox加载wms的效果
该方法加载有点慢 提前跟你们声明一下
1.2mapbox加载wms的源码
<template>
<div class="mapbox-maps">
<div id="map"></div>
</div>
</template>
<script>
import mapboxgl from './../../public/mapbox-gl.js'
import './../../public/mapbox-gl.css' // 一定要引入样式, 否则有些东西显示不出来(比如导航控制条)
//import MapboxLanguage from '@mapbox/mapbox-gl-language'
// import mapboxgl from 'mapbox-gl'
// import 'mapbox-gl/dist/mapbox-gl.css'
export default {
methods: {
initMap() {
// application/vnd.mapbox-vector-tile,GEoServer版本不同,这里可能不同,注意在GeoServer里查看
//mapboxgl.accessToken = 'pk.eyJ1IjoibW9ob25nIiwiYSI6ImNrNGFsdjY5ZzA1NW4zbG14b2JoMnA5c3IifQ.1qVWFsyHW2wKThTgQg08SA';
var wmsUrl = "http://192.168.10.10:8080/geoserver/mymvt/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&format=image/png&TRANSPARENT=true&STYLES&LAYERS=mymvt:CSZTGH&exceptions=application/vnd.ogc.se_inimage&srs=EPSG:900913&WIDTH=768&HEIGHT=370&BBOX={bbox-epsg-3857}";
var map = new mapboxgl.Map({
container: 'map',
style: {
"version": 8,
"name": "test-wms-style",
"sources": {
"osm-wms": {
'type': 'raster',
// 'scheme': 'tms',//TMS服务有此一行,默认xyz
'tiles': [
wmsUrl
],
//"tileSize": 256
}
},
"layers": [{
"id": "wms-test-id",
"type": "raster",
"source": "osm-wms",
//"source-layer": "CSZTGH",
"paint": {
'raster-opacity': 1
}
}]
},
zoom: 12,
center: [114.9, 27]
});
}
},
mounted() {
this.initMap()
}
}
</script>
<style lang="less">
.mapbox-maps {
width: 100%;
height: calc(100vh - 50px);
position: relative;
#map {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
.mapboxgl-canvas {
outline: none;
}
// 隐藏mapbox商标
.mapboxgl-ctrl-logo {
display: none;
}
}
}
</style>
2.mapbox加载geoserver的矢量wmts格式
2.1mapbox加载矢量wmts的效果
2.2mapbox加载矢量wmts的源码
<template>
<div class="mapbox-maps">
<div id="map"></div>
</div>
</template>
<script>
import mapboxgl from './../../public/mapbox-gl.js'
import './../../public/mapbox-gl.css' // 一定要引入样式, 否则有些东西显示不出来(比如导航控制条)
//import MapboxLanguage from '@mapbox/mapbox-gl-language'
// import mapboxgl from 'mapbox-gl'
// import 'mapbox-gl/dist/mapbox-gl.css'
export default {
methods: {
initMap() {
// application/vnd.mapbox-vector-tile,GEoServer版本不同,这里可能不同,注意在GeoServer里查看
//mapboxgl.accessToken = 'pk.eyJ1IjoibW9ob25nIiwiYSI6ImNrNGFsdjY5ZzA1NW4zbG14b2JoMnA5c3IifQ.1qVWFsyHW2wKThTgQg08SA';
var vectorwmtsLayerUrl = "http://192.168.10.10:8080/geoserver/gwc/service/wmts?" +
"REQUEST=GetTile&SERVICE=WMTS&VERSION=1.0.0&LAYER=mymvt:CSZTGH" +
"&STYLE=&TILEMATRIX=EPSG:900913:{z}&TILEMATRIXSET=EPSG:900913&" +
"FORMAT=application/vnd.mapbox-vector-tile&TILECOL={x}&TILEROW={y}";
var map = new mapboxgl.Map({
container: 'map',
style: {
"version": 8,
"name": "test-wmts-pbf-style",
"sources": {
"osm-wmts-vec": {
'type': 'vector',
// 'scheme': 'tms',//TMS服务有此一行,默认xyz
'tiles': [
vectorwmtsLayerUrl
],
//"tileSize": 256
}
},
"layers": [{
"id": "wmts-vec-id",
"type": "line",
"source": "osm-wmts-vec",
"source-layer": "CSZTGH",
"layout": {
"line-join": "round",
"line-cap": "round"
},
//样式
"paint": {
"line-color": "#E31asa",
"line-width": 1,
"line-opacity": 1
}
}]
},
zoom: 12,
center: [114.9, 27]
});
}
},
mounted() {
this.initMap()
}
}
</script>
<style lang="less">
.mapbox-maps {
width: 100%;
height: calc(100vh - 50px);
position: relative;
#map {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
.mapboxgl-canvas {
outline: none;
}
// 隐藏mapbox商标
.mapboxgl-ctrl-logo {
display: none;
}
}
}
</style>
3.mapbox加载geoserver的tms格式
3.1mapbox加载tms的效果
前提开启pplication/vnd.mapbox-vector-tile
3.2mapbox加载tms的源码
<template>
<div class="mapbox-maps">
<div id="map"></div>
</div>
</template>
<script>
import mapboxgl from './../../public/mapbox-gl.js'
import './../../public/mapbox-gl.css' // 一定要引入样式, 否则有些东西显示不出来(比如导航控制条)
//import MapboxLanguage from '@mapbox/mapbox-gl-language'
// import mapboxgl from 'mapbox-gl'
// import 'mapbox-gl/dist/mapbox-gl.css'
export default {
methods: {
initMap() {
// application/vnd.mapbox-vector-tile,GEoServer版本不同,这里可能不同,注意在GeoServer里查看
//mapboxgl.accessToken = 'pk.eyJ1IjoibW9ob25nIiwiYSI6ImNrNGFsdjY5ZzA1NW4zbG14b2JoMnA5c3IifQ.1qVWFsyHW2wKThTgQg08SA';
var tmsUrl = "http://192.168.10.10:8080/geoserver/gwc/service/tms/1.0.0/mymvt%3ACSZTGH@EPSG%3A900913@pbf/{z}/{x}/{y}.pbf";
var map = new mapboxgl.Map({
container: 'map',
style: {
"version": 8,
"name": "test-tms-style",
"sources": {
"osm-tms": {
'type': 'vector',
'scheme': 'tms',//TMS服务有此一行,默认xyz
'tiles': [
tmsUrl
],
//"tileSize": 256
}
},
"layers": [{
"id": "tms-test-id",
"type": "line",
"source": "osm-tms",
"source-layer": "CSZTGH",
"layout": {
"line-join": "round",
"line-cap": "round"
},
"paint": {
"line-color": "#E31A1C",//红色
"line-width": 3,
"line-opacity": 1
}
}]
},
zoom: 12,
center: [114.9, 27]
});
}
},
mounted() {
this.initMap()
}
}
</script>
<style lang="less">
.mapbox-maps {
width: 100%;
height: calc(100vh - 50px);
position: relative;
#map {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
.mapboxgl-canvas {
outline: none;
}
// 隐藏mapbox商标
.mapboxgl-ctrl-logo {
display: none;
}
}
}
</style>
4.mapbox加载geoserver的静态切片wmts格式
4.1mapbox加载静态切片wmts的效果
4.2mapbox加载静态切片wmts的源代码
<template>
<div class="mapbox-maps">
<div id="map"></div>
</div>
</template>
<script>
import mapboxgl from './../../public/mapbox-gl.js'
import './../../public/mapbox-gl.css' // 一定要引入样式, 否则有些东西显示不出来(比如导航控制条)
//import MapboxLanguage from '@mapbox/mapbox-gl-language'
// import mapboxgl from 'mapbox-gl'
// import 'mapbox-gl/dist/mapbox-gl.css'
export default {
methods: {
initMap() {
//注意 EPSG:900913相当于3857坐标系 因为他们参数一致
var wmtsStyle = "http://192.168.10.10:8080/geoserver/gwc/service/wmts?layer=mymvt:CSZTGH&style=&tilematrixset=EPSG:900913&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image/png&TileMatrix=EPSG:900913:{z}&TileCol={x}&TileRow={y}";
// mapboxgl.accessToken ='pk.eyJ1IjoibWFwYm94bWF4IiwiYSI6ImNqbnY4MHM3azA2ZmkzdnBnMThvNzRoZ28ifQ.IffqPZGkhcdPjnZ2dmSO6w';
var map = new mapboxgl.Map({
container: 'map',
style: {
"version": 8,
"name": "test-wmts-style",
"sources": {
"osm-wmts": {
"type": "raster",//栅格格式相当于图片
'tiles': [
wmtsStyle//geoserver服务地址
],
"tileSize": 256//瓦片大小
}
},
"layers": [{
"id": "wmts-test-id",//相当于图层名字
"type": "raster",//栅格格式相当于图片
"source": "osm-wmts",//对应sources取得键名
"minZoom": 1,
"maxZoom": 18,
"paint": {
"raster-opacity": 1//绘画透明度 1不透明
}
}]
},
zoom: 3,
center: [112.08, 27.54]
});
}
},
mounted() {
this.initMap()
}
}
</script>
<style lang="less">
.mapbox-maps {
width: 100%;
height: calc(100vh - 50px);
position: relative;
#map {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
.mapboxgl-canvas {
outline: none;
}
// 隐藏mapbox商标
.mapboxgl-ctrl-logo {
display: none;
}
}
}
</style>
<!-- <template>
<div class="map_box">
<div ref="map" class="map" id="map"></div>
</div>
</template>
<script>
import maxboxMap from './futureMap.js'
export default {
components: {},
data() {
return {
map: null,
}
},
computed: {},
methods: {
handleInitMap() {
// 后期想传入一些值 比如areaCode 之类的 自动定位至改城市的中心
let map = new maxboxMap({
element: 'map'
})
},
},
watch: {},
mounted() {
this.handleInitMap()
},
}
</script> -->
<style lang="less"></style>