Mapbox加载天地图CGCS2000矢量瓦片地图

1.背景

最近在做天地图的项目,要基于MapBox添加CGCS2000矢量切片数据,但是 Mapbox 只支持web 墨卡托(3857)坐标系的数据。Github有专业用户修改了mapbox-gl的相关代码,支持CGCS2000的切片数据加载,并且修改了相关的mapbox-gl的配套代码,详情请见github网址。https://github.com/cgcs2000

2.将MapBox部署到本地

npm i @cgcs2000/mapbox-gl
<!DOCTYPE html>
<html>

<head>
    <meta charset='utf-8' />
    <title>Mapbox加载天地图CGCS2000矢量瓦片地图服务</title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <style>
        body {
            margin: 0;
            padding: 0;
        }

        #map {
            position: absolute;
            top: 0;
            bottom: 0;
            width: 100%;
        }
    </style>
</head>

<body>
    <div id='map'></div>
</body>
<script type="module">
import '@cgcs2000/mapbox-gl/dist/mapbox-gl.css'
import mapboxgl from '@cgcs2000/mapbox-gl';

mapboxgl.accessToken = 'YourToken';
//添加天地图
var mapStyle = 
	{
	"version": 8,
	"name": "Map",
	"sources": {
		"world-vec": {
			"type": "raster",
			"tiles": [
				"http://t0.tianditu.gov.cn/vec_c/wmts?tk=......"
			],
			"scheme": "xyz",
			"maxzoom": 9,
			"tileSize": 256
		},
	"layers": [
		{
			"id": "world-vec",
			"source": "world-vec",
			"type": "raster",
			"layout": {
				"visibility": "visible"
			}
		},
	],
	"glyphs": "mapbox://fonts/mapbox/{fontstack}/{range}.pbf",
	"transition": {
		"duration": 300,
		"delay": 0
	}
}

var map = new mapboxgl.Map({
	container: 'map',
	zoom: 7,
	center: [118.8, 36.3],
	style: mapStyle,
});

/**CGCS2000投影坐标系(4490)**/
map.on('load', function loaded() {
	//添加geoserver矢量切片
	map.addSource('custom-go-vector-tile-source', {
		type: 'vector',
		scheme: 'tms',
		tiles: ['http://localhost:8080/geoserver/gwc/service/tms/1.0.0/MyWork%3Acounty@EPSG%3A4490@pbf/{z}/{x}/{y}.pbf'], //GeoServer发布的矢量切片地图
		zoomOffset: -1

	});
	//配置矢量切片图层样式
	map.addLayer({
		"id": "custom-go-vector-tile-layer",
		"type": "circle",       // 符号化样式 [fill,line,symbol,circle,heatmap]
		"source": "custom-go-vector-tile-source",
		"source-layer": "county",  //添加矢量图层名
		"minzoom": 5,
		"maxzoom": 13,
		 paint: {
			'circle-radius': 1,
			'circle-color': "#000000",
			'circle-opacity': 0.8
		}
	});

});

</script>
</html>

3.GeoServer发布CGCS2000的矢量瓦片服务

1.安装过程自行百度,Tomcat下面部署Geoserver可能会遇到跨域访问问题。将geoserver部署到tomcat之后,打开tomcat下webapps\geoserver\WEB-INF目录下的web.xml文件,添加一下内容,重启tomcat即可。

    <filter>
      <filter-name>CORS</filter-name> 
      <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
      <init-param> 
       <param-name>cors.tagRequests</param-name> 
          <param-value>true</param-value> 
      </init-param>
    </filter>
    <filter-mapping> 
      <filter-name>CORS</filter-name> 
      <url-pattern>/*</url-pattern> 
    </filter-mapping>

2.发布矢量瓦片服务
GeoServer默认只有84的坐标系,我们需要自己定义CGCS2000的坐标系。
首先,点击Tile Caching下的Gridsets,创建一个新的坐标系
在这里插入图片描述
在框选的地方搜索4490,选择为CGCS2000坐标系,其他数据按图填写(279,541,132.0143589),级别自定义。
在这里插入图片描述
设置完成后在矢量切片设置页,将新设置坐标系添加进来
在这里插入图片描述
在TMS页就可以看到服务地址
在这里插入图片描述

4.效果展示

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Mapbox GL JS 是一款用于在 Web 上呈现交互式地图的开源 JavaScript 库。Mapbox GL JS 能够加载多种矢量瓦片数据,包括 gcj02 矢量瓦片数据。 要加载 gcj02 矢量瓦片数据,需要使用 Mapbox GL JS 的 `mapbox-gl-native` 库,并在代码中指定数据的坐标系。以下是加载 gcj02 矢量瓦片数据的示例代码: ```javascript mapboxgl.accessToken = 'your-access-token'; var map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/mapbox/streets-v11', center: [116.4074, 39.9042], zoom: 10 }); // 加载 gcj02 矢量瓦片数据 map.on('load', function() { map.addSource('gcj02-tiles', { 'type': 'vector', 'tiles': ['http://your-tile-server.com/{z}/{x}/{y}.pbf'], 'minzoom': 0, 'maxzoom': 22, 'bounds': [72.004, 0.8293, 137.8347, 55.8271], 'scheme': 'xyz', 'tileSize': 256, 'promoteId': { 'gcj02': 'id' } }); map.addLayer({ 'id': 'gcj02-layer', 'type': 'fill', 'source': 'gcj02-tiles', 'source-layer': 'your-source-layer', 'paint': { 'fill-color': '#f00', 'fill-opacity': 0.5 } }); }); ``` 在上述代码中,`tiles` 指定了 gcj02 矢量瓦片数据的 URL,`bounds` 指定了数据的范围,`promoteId` 指定了数据中的 `gcj02` 字段作为要素的唯一标识符。最后,使用 `map.addLayer` 方法将数据添加地图中,并设置样式。 需要注意的是,由于 gcj02 是中国特有的坐标系,因此在加载 gcj02 矢量瓦片数据时,需要使用特定的坐标转换算法将其转换为 WGS84 或 Web Mercator 等国际通用的坐标系。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

王八八。

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

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

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

打赏作者

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

抵扣说明:

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

余额充值