跟牛老师一起学WEBGIS——WEBGIS实现(绘制切片)

概述

前面已经有三篇文章分别讲述了线和面图片的绘制,在本文讲讲如何实现切片的绘制。

5. 绘制切片

前面的文章地图切片提到了切片的几个重要参数:切片范围、切片原点、分辨率,这些概念在实现切片的调用的时候都会用到,下面为实现代码:

/**
 * 在地图上展示切片
 * @private
 */
TileLayer.prototype._showTile2Map = function () {
    const that = this;
    let extent = that._map.getBounds().toArray();
    const projMin = turf.projection.toMercator(extent[0]);
    const projMax = turf.projection.toMercator(extent[1]);
    const zoom = Math.ceil(that._map.getZoom());
    const res = that._tileParams.zoomRes[zoom];
    const tileSize = this._params.tileSize || 256;
    const xmin = Math.floor((projMin[0] - that._tileParams.tileStart[0]) / (res * tileSize));
    const xmax = Math.ceil((projMax[0] - that._tileParams.tileStart[0]) / (res * tileSize));
    const ymin = Math.floor((that._tileParams.tileStart[1] - projMax[1]) / (res * tileSize));
    const ymax = Math.ceil((that._tileParams.tileStart[1] - projMin[1]) / (res * tileSize));
    for (let x = xmin; x <= xmax; x++) {
        for (let y = ymin; y <= ymax; y++) {
            that._addTile2Map(x, y, zoom);
        }
    }
}
/**
 * 展示单个切片
 * @param x
 * @param y
 * @param z
 * @private
 */
TileLayer.prototype._addTile2Map = function (x, y, z) {
    const that = this;
    const tileSize = that._params.tileSize || 256;
    const res = that._tileParams.zoomRes[z];
    const xmin = that._tileParams.tileStart[0] + x * (res * tileSize);
    const xmax = that._tileParams.tileStart[0] + (x + 1) * (res * tileSize);
    const ymax = that._tileParams.tileStart[1] - y * (res * tileSize);
    const ymin = that._tileParams.tileStart[1] - (y + 1) * (res * tileSize);
    const min = turf.projection.toWgs84([xmin, ymin]);
    const max = turf.projection.toWgs84([xmax, ymax]);
    const pixelMin = that._map2pixel(min);
    const pixelMax = that._map2pixel(max);
    const width = Math.abs(pixelMax.x - pixelMin.x);
    const height = Math.abs(pixelMax.y - pixelMin.y);
    let url = that._params.url;
    url = url.replace('{x}', x);
    url = url.replace('{y}', y);
    url = url.replace('{z}', z);
    const img = new Image();
    img.src = url;
    img.setAttribute('crossOrigin', 'anonymous');
    img.onload = function () {
        that._ctx.drawImage(img, pixelMin.x, pixelMax.y, width, height);
    }
}
/**
 * 初始化切片参数
 * @private
 */
TileLayer.prototype._initTileParams = function () {
    const minZoom = 0;
    const maxZoom = 18;
    const originExtent = [-20037508.34, -20037508.34, 20037508.34, 20037508.34];
    const tileSize = this._params.tileSize || 256;
    let tileParams = {
        originExtent: originExtent,
        tileStart: [originExtent[0], originExtent[3]],
        zoomRes: {}
    };
    for(let i = minZoom; i <= maxZoom; i++) {
        const res0 = (originExtent[2] - originExtent[0]) / tileSize;
        tileParams.zoomRes[i] = res0 / Math.pow(2, i);
    }
    this._tileParams = tileParams;
}

上面的代码中_initTileParams是以标准的谷歌切片的参数。切片调用的是谷歌的标注图层,实现后的效果如下:
image.png

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

牛老师讲GIS

感谢老板支持

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

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

打赏作者

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

抵扣说明:

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

余额充值