Openlayers实例-加载天地图

天地图地图服务采用OGCWMTS标准,使用Openlayers可以很方便的对天地图的图层进行叠加,天地图地图服务对所有用户开放。用户只需注册一个账号,申请一个key就可以使用天地图提供的服务地址了。天地图提供的服务列表主要包括矢量底图矢量注记影像底图影像注记地形晕渲地形注记全球境界矢量英文注记影像英文注记三维地名三维地形等。投影包括经纬度投影球面墨卡托投影。请求方式主要包括地图瓦片元数据两种,下面分别介绍下这两种请求方式。

1.地图瓦片

地图瓦片加载方式,主要是使用ol.source.XYZ作为数据源,加载到ol.layer.Tile图层上。天地图瓦片请求的地址如下所示:

http://t0.tianditu.gov.cn/img_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={x}&TILECOL={y}&tk=您的密钥

请求图层的相关代码如下所示:

import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import { get as getProjection } from 'ol/proj';

static createTDTLayer(url, options) {
        const epsg = 'EPSG:4326';
        const projection = getProjection(epsg);
        const source = new XYZ({
            url,
            projection,
            ...options,
        });
        const layer = new TileLayer({
            source,
        });
        return layer;
    }

请求的数据如果是用的是经纬度投影,在实例化source的时候,需要设置投影类型。

2.元数据加载

天地图除了使用瓦片进行加载外,还提供了WMTS的加载方式,具体的加载代码如下所示:

import TileLayer from 'ol/layer/Tile';
import { get as getProjection } from 'ol/proj';
import WMTS from 'ol/source/WMTS';
import { get as getProjection } from 'ol/proj';
import { getWidth, getTopLeft } from 'ol/extent';
import WMTSTileGrid from 'ol/tilegrid/WMTS';
static createTdtWMTSLayer() {
        const epsg = 'EPSG:4326';
        const projection = getProjection(epsg);
        const projectionExtent = projection.getExtent();
        const size = getWidth(projectionExtent) / 256;
        const length = 19;
        const resolutions = new Array(length);
        const matrixIds = new Array(length);
        for (let i = 0; i < length; i += 1) {
            const pow = Math.pow(2, i);
            resolutions[i] = size / pow;
            matrixIds[i] = i;
        }
        const source = new WMTS({
            url: 'http://t3.tianditu.com/img_c/wmts?tk=申请的key',
            layer: 'img',
            style: 'default',
            matrixSet: 'c',
            format: 'tiles',
            wrapX: true,
            tileGrid: new WMTSTileGrid({
                origin: getTopLeft(projectionExtent),
                resolutions,
                matrixIds,
            }),
        });
        const layer = new TileLayer({
            source,
        });
        return layer;
    }

个人博客

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Samples 通过EPSG.io搜索进行二次投影 视图(View)动画 使用动态 ArcGIS REST Map...
    changhr2013阅读 15,460评论 0 8
  • 内容摘要:作图需要数据,要把本地的数据资源进行共享,或者从服务端获取地图数据。经常需要面对各种地图服务规范,例如:...
    地学小哥阅读 1,329评论 0 6
  • 通过前面几篇文章的介绍后,对Openlayers的基本用法都有所了解了,从这篇开始主要来介绍API的使用,介绍完A...
    写前端的大叔阅读 29,393评论 0 15
  • 笔者最近实践了利用openlayers在android app中实现离线地图应用,本文记录了笔者实践的思路。 an...
    老羽阅读 10,776评论 9 6
  • 4/100 2019年6月16日星期日 多云读经人员:飞儿(家长)累积时间:100天挑战第4天诵读内容:《唐诗三百...
    惠柔育儿阅读 181评论 0 1