vue3-openlayers 使用tianditu,wmts和xyz等source加载天地图切片服务

本篇介绍一下使用vue3-openlayers加载天地图切片,三种方法:

  1. 使用tianditu(ol-source-tianditu内部实现其实用的wmts)
  2. 使用wmts(ol-source-wmts)
  3. 使用xyz(ol-source-xyz)

1 需求

  • vue3-openlayers加载天地图

2 分析

主要是不同类型source的使用

3 实现

3.1 ol-source-tianditu

javascript 复制代码
<template>
  <ol-map
    :loadTilesWhileAnimating="true"
    :loadTilesWhileInteracting="true"
    style="width: 100%; height: 100%"
		ref="mapRef"
  >
    <ol-view
      ref="view"
      :center="center"
      :rotation="rotation"
      :zoom="zoom"
      :projection="projection"
    />

    <ol-tile-layer>
      <ol-source-tianditu
        layerType="img"
        :projection="projection"
        :tk="key"
        :hidpi="true"
				ref="sourceRef" 
      ></ol-source-tianditu>
    </ol-tile-layer>
    <ol-tile-layer>
      <ol-source-tianditu
			 :isLabel="true"
        layerType="img"
        :projection="projection"
        :tk="key"
        :hidpi="true"
      ></ol-source-tianditu>
    </ol-tile-layer>
		<!-- 下面的注记加载等价于上面的 -->
		<!-- <ol-tile-layer>
      <ol-source-tianditu
        layerType="cia"
        :projection="projection"
        :tk="key"
        :hidpi="true"
      ></ol-source-tianditu>
    </ol-tile-layer> -->
  </ol-map>
</template>

<script setup lang="ts">

const center = ref([121, 31]);
const projection = ref('EPSG:4326');
const zoom = ref(5);
const rotation = ref(0);
const mapRef=ref();
const key = '换成申请的天地图key';
const sourceRef = ref(null);
// layerType  img, vec, ter, cia, cta
//  'vec', 'cva'  矢量底图, 矢量注记
//  'img', 'cia'  影像底图, 影像注记
//  'ter', 'cta'  地形晕渲, 地形注记

onMounted(() => {
  const source = sourceRef.value?.source;
	// 调用source上的方法
});
</script>
<style scoped lang="scss">
</style>

3.2 wmts

javascript 复制代码
<template>
  <ol-map
    :loadTilesWhileAnimating="true"
    :loadTilesWhileInteracting="true"
    style="width: 100%; height: 100%"
    ref="mapRef"
  >
    <ol-view
      ref="view"
      :center="center"
      :rotation="rotation"
      :zoom="zoom"
      :projection="projection"
    />

    <ol-tile-layer>
      <ol-source-wmts
        ref="sourceRef"
        :attributions="attribution"
        :url="url"
        :projection="projection"
        :matrixSet="matrixSet"
        :format="format"
        :layer="layerImage"
        :styles="styleName"
      ></ol-source-wmts>
    </ol-tile-layer>
    <ol-tile-layer>
      <ol-source-wmts
        :attributions="attribution"
        :url="url_address"
        :projection="projection"
        :matrixSet="matrixSet"
        :format="format"
        :layer="layerAddress"
        :styles="styleName"
      ></ol-source-wmts>
    </ol-tile-layer>
  </ol-map>
</template>

<script setup lang="ts">
const center = ref([121, 31]);
const projection = ref('EPSG:4326');
const zoom = ref(5);
const rotation = ref(0);
const mapRef = ref();
const format = ref('image/png');
const styleName = ref('default');
const attribution = ref('Tiles © <a href="https://www.tianditu.gov.cn/">天地图</a>');
const key = '换成申请的天地图key';
const sourceRef = ref(null);

const layerImage = ref('img');
const layerAddress = ref('cia');
const matrixSet = ref('c');//c: 经纬度投影 w: 墨卡托投影
//  'vec', 'cva'  矢量底图, 矢量注记
//  'img', 'cia'  影像底图, 影像注记
//  'ter', 'cta'  地形晕渲, 地形注记

// 影像底图
const url = ref(`http://t{0-6}.tianditu.com/${layerImage.value}_${matrixSet.value}/wmts?tk=${key}`);
// 影像注记
const url_address = ref(
  `http://t{0-6}.tianditu.com/${layerAddress.value}_${matrixSet.value}/wmts?tk=${key}`
);

onMounted(() => {
	const map=mapRef.value?.map
  const source = sourceRef.value?.source;
});
</script>
<style scoped lang="scss"></style>

</script>
<style scoped lang="scss">
</style>

3.3 xyz

javascript 复制代码
<template>
  <ol-map
    :loadTilesWhileAnimating="true"
    :loadTilesWhileInteracting="true"
    style="width: 100%; height: 100%"
    ref="mapRef"
  >
    <ol-view
      ref="view"
      :center="center"
      :rotation="rotation"
      :zoom="zoom"
      :projection="projection"
    />
		
    <ol-tile-layer>
      <ol-source-xyz :projection="projection" :url="imageUrl" />
    </ol-tile-layer>
		<ol-tile-layer :opacity="0.9">
      <ol-source-xyz :projection="projection" :url="addressUrl" />
    </ol-tile-layer>
  </ol-map>
</template>

<script setup lang="ts">
const center = ref([121, 31]);
const projection = ref('EPSG:4326');
const zoom = ref(5);
const rotation = ref(0);
const mapRef = ref();
const key = '换成申请的天地图key';
const sourceRef = ref(null);

const layerImage = ref('img');
const layerAddress = ref('cia');
const matrixSet = ref('c');//c: 经纬度投影 w: 墨卡托投影
//  'vec', 'cva'  矢量底图, 矢量注记
//  'img', 'cia'  影像底图, 影像注记
//  'ter', 'cta'  地形晕渲, 地形注记

// 影像底图
const imageUrl = ref(
  `https://t{0-7}.tianditu.gov.cn/DataServer?T=${layerImage.value}_${matrixSet.value}&tk=${key}&x={x}&y={y}&l={z}`
);
// 影像注记
const addressUrl = ref(
  `https://t{0-7}.tianditu.gov.cn/DataServer?T=${layerAddress.value}_${matrixSet.value}&tk=${key}&x={x}&y={y}&l={z}`
);

onMounted(() => {
  const map = mapRef.value?.map;
  const source = sourceRef.value?.source;
});
</script>
<style scoped lang="scss"></style>

加载注记图层后,切片虽然显示png,但是好像不是透明的,只能在注记图层增加透明度控制

相关推荐
灰海4 小时前
vue中通过heatmap.js实现热力图(多个热力点)热区展示(带鼠标移入弹窗)
前端·javascript·vue.js·heatmap·heatmapjs
王源骏4 小时前
LayaAir鼠标(手指)控制相机旋转,限制角度
前端
大虾写代码4 小时前
vue3+TS项目配置Eslint+prettier+husky语法校验
前端·vue·eslint
wordbaby5 小时前
用 useEffectEvent 做精准埋点:React analytics pageview 场景的最佳实践与原理剖析
前端·react.js
上单带刀不带妹5 小时前
在 ES6 中如何提取深度嵌套的对象中的指定属性
前端·ecmascript·es6
excel5 小时前
使用热力贴图和高斯函数生成山峰与等高线的 WebGL Shader 解析
前端
wyzqhhhh5 小时前
组件库打包工具选型(npm/pnpm/yarn)的区别和技术考量
前端·npm·node.js
码上暴富5 小时前
vue2迁移到vite[保姆级教程]
前端·javascript·vue.js
土了个豆子的6 小时前
04.事件中心模块
开发语言·前端·visualstudio·单例模式·c#
全栈技术负责人6 小时前
Hybrid应用性能优化实战分享(本文iOS 与 H5为例,安卓同理)
前端·ios·性能优化·html5