作者:yx
平常我们使用更多的是256* 256尺寸的瓦片出图,但有的客户需要以512* 512尺寸大小的瓦片出图,对于leaflet可以直接修改tilesize参数为512,例如:
但是对于MapboxGL而言修改tilesize参数为512是不生效的:
是因为mapbox原生的问题,512比256zoom要低一级,需要对请求地址的z值进行处理,接下来一起看看如何处理
transformRequest: (url, resourceType) => {
if (resourceType === 'Tile') {
let zStr = url.substr(url.indexOf("z=") + 2, url.length).split("&")[0]
return {
url: url.replace(`z=${zStr}`, `z=${(Number(zStr) + 1).toString()}`)
}}
利用这个方法,将Z值解析出来,然后对Z值进行加一的操作。
<!--********************************************************************
* Copyright? 2000 - 2022 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'/>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no'/>
<title data-i18n="resources.title_tiledMapLayer"></title>
<script type="text/javascript" src="../js/include-web.js"></script>
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<div id='map'></div>
<script type="text/javascript" exclude='iclient-mapboxgl' src="../../dist/mapboxgl/include-mapboxgl.js"></script>
<script type="text/javascript">
var host = window.isLocal ? window.server : "https://iserver.supermap.io";
var attribution = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>? Mapbox </a>" +
" with <span>? <a href='https://iclient.supermap.io' target='_blank'>SuperMap iClient</a> | </span>" +
" Map Data <span>? <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> ";
var map = new mapboxgl.Map({
container: 'map', // container id
style: {
"version": 8,
"sources": {
"raster-tiles": {
"attribution": attribution,
"type": "raster",
"tiles": [host + '/iserver/services/map-china400/rest/maps/China/zxyTileImage.png?width=512&height=512&z={z}&x={x}&y={y}'],
"tileSize":512
}
},
"layers": [{
"id": "simple-tiles",
"type": "raster",
"source": "raster-tiles",
"minzoom": 0,
"maxzoom": 22
}]
},
center: [120.143, 30.236], // starting position
zoom: 3 ,// starting zoom
transformRequest: (url, resourceType) => {
if (resourceType === 'Tile') {
let zStr = url.substr(url.indexOf("z=") + 2, url.length).split("&")[0]
return {
url: url.replace(`z=${zStr}`, `z=${(Number(zStr) + 1).toString()}`)
};
}
}
});
map.addControl(new mapboxgl.NavigationControl(), 'top-left');
</script>
</body>
</html>