欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > openlayers 自定义瓦片(切片)颜色

openlayers 自定义瓦片(切片)颜色

2024/10/24 1:58:00 来源:https://blog.csdn.net/weixin_56624286/article/details/139965774  浏览:    关键词:openlayers 自定义瓦片(切片)颜色

本篇介绍一下使用openlayers 自定义瓦片(切片)颜色

1 需求

  • 自定义瓦片(切片)颜色

2 分析

使用各种source的tileLoadFunction属性

4 实现

修改前:

修改后:

<template><div id="map" class="map"></div>
</template><script setup lang="ts">
import { Map, View } from 'ol';
import { Tile as TileLayer } from 'ol/layer';
import { get } from 'ol/proj';
import { XYZ } from 'ol/source';const projection = get('EPSG:4326');const layerTypeMap = {vector: ['vec', 'cva'], // [矢量底图, 矢量注记]image: ['img', 'cia'], // [影像底图, 影像注记]terrain: ['ter', 'cta'] // [地形晕渲, 地形注记]
};const map = ref();onMounted(() => {initMap('image');
});const initMap = (layerType = 'image') => {const key = '替换为天地图key';// c: 经纬度投影 w: 墨卡托投影const matrixSet = 'c';map.value = new Map({target: 'map',layers: [// 底图new TileLayer({source: new XYZ({url: `https://t{0-7}.tianditu.gov.cn/DataServer?T=${layerTypeMap[layerType][0]}_${matrixSet}&tk=${key}&x={x}&y={y}&l={z}`,projection,tileLoadFunction:handleTileLoadFunction})}),// 注记new TileLayer({source: new XYZ({url: `https://t{0-7}.tianditu.gov.cn/DataServer?T=${layerTypeMap[layerType][1]}_${matrixSet}&tk=${key}&x={x}&y={y}&l={z}`,projection,})})],view: new View({center: [116.406393, 39.909006],projection: projection,zoom: 5,maxZoom: 17,minZoom: 1})});
};const handleTileLoadFunction=(imageTile: any, src: string) => {// 该函数默认为imageTile.getImage().src = src;// 以下为自定义let img = new Image();img.setAttribute('crossOrigin', 'Anonymous');img.src = src;img.onload = () => {let canvas = document.createElement('canvas');let w = img.width;let h = img.height;canvas.width = w;canvas.height = h;let context = canvas.getContext('2d');context!.filter = 'hue-rotate(100deg)';context?.drawImage(img, 0, 0, w, h, 0, 0, w, h);const imageData = context!.getImageData(0, 0, canvas.width, canvas.height);const pixelData = imageData?.data ?? [];// pixelData 为数组 是[r,g,b,a]的循环结构for (let i = 0; i < pixelData.length; i++) {// pixelData[i * 4 + 0] r 通道;// pixelData[i * 4 + 1] g 通道;// pixelData[i * 4 + 2] b 通道;// pixelData[i * 4 + 3] a 通道;}context!.putImageData(imageData, 0, 0, 0, 0, canvas.width, canvas.height);imageTile.getImage().src = canvas.toDataURL('image/png');};};</script>
<style scoped lang="scss">
.map {width: 100%;height: 100%;
}
</style>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com