MapBox本地化加载GeoServer的wms,tms,矢量wmts,静态切片wmts四种格式

注意: 首先要破解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>

### GeoServer 图层预览配置教程 GeoServer 是一个开源软件服务器,允许用户通过标准协议共享地理空间数据。以下是关于如何在 GeoServer 中配置并查看图层预览的相关信息。 #### 1. 地理数据导入与发布 要使图层能够被预览,首先需要将地理数据(如 Shapefile 或数据库表)导入到 GeoServer 并发布它。具体操作如下: - 登录到 GeoServer 的 Web 界面。 - 进入 **Data → Stores** 页面,点击 “Add new Store”,选择适合的数据存储类型(例如 PostGIS 数据库或文件系统上的 Shapefile 文件夹)。 - 完成数据源创建后,进入 **Layers** 页面,找到对应的工作区下的新数据集,并将其作为图层发布[^1]。 #### 2. 配置 WMS/WMTS/TMS 输出参数 为了支持不同客户端访问方式以及优化性能表现,可以调整以下几个方面设置来增强用户体验效果: - 在 Layer Configuration 下拉菜单里修改请求 URL 和默认样式选项; - 如果希望提供瓦片缓存功能,则需启用 GeoWebCache (GWC),并通过编辑 `web.xml` 添加自定义磁盘路径保存生成好的切片文件[^3]: ```xml <context-param> <param-name>GEOWEBCACHE_CACHE_DIR</param-name> <param-value>/path/to/cache/directory</param-value> </context-param> ``` #### 3. 访问图层预览页面 完成以上准备工作之后就可以直接打开浏览器输入类似下面这样的链接地址来进行在线浏览测试了: ``` http://localhost:8080/geoserver/web/?wicket:bookmarkablePage=:org.geoserver.web.demo.MapPreviewPage ``` 此界面会展示当前可用的所有公开资源列表及其基本信息摘要;选中目标项即可加载其实际渲染效果图样^。 另外值得注意的一点是当涉及到跨域资源共享(CORS)时可能还需要额外处理才能让前端应用顺利调用后台接口返回的地图内容[^1]^。 ```javascript // Example of using Mapbox GL JS to load a WMS service from GeoServer. map.addSource('my-wms-source', { type: 'raster', tiles: [ 'http://localhost:8080/geoserver/wms?service=WMS&version=1.1.1&request=GetMap&layers=test_layer&styles=&bbox={bbox-epsg-3857}&width=256&height=256&srs=EPSG:3857&format=image/png' ], tileSize: 256 }); map.addLayer({ id: 'my-wms-layer', type: 'raster', source: 'my-wms-source' }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

挣钱花388

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值