欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > leaflet 加载天地图改变底图颜色

leaflet 加载天地图改变底图颜色

2024/10/25 6:28:51 来源:https://blog.csdn.net/qq_42657934/article/details/142449526  浏览:    关键词:leaflet 加载天地图改变底图颜色

最近在开发大屏的时候用到了leaflet 和天地图,但是加载出来的天地图是白色的 , 与大屏风格不统一
,最后经过一系列调整改为蓝色风格,效果如下图
在这里插入图片描述

      实现思路: 通过应用图层透明度,设置容器背景色而实现。是不是非常简单高效,嘻嘻ps: 开发前提 在天地图官网注册登录申请地图服务需要的key:[天地图控制台](https://console.tianditu.gov.cn/api/key)

在这里插入图片描述

主要代码如下:
在这里插入图片描述
源代码:src\utils\leaflet\j-map.js

import * as L from "leaflet";
export default function initMap(containerId, centralPoint, zoom, zoomSnap) {L.TileLayer.ChinaProvider = L.TileLayer.extend({initialize: function (type, options) { // (type, Object)const providers = L.TileLayer.ChinaProvider.providers;const parts = type.split('.');const providerName = parts[0];const mapName = parts[1];const mapType = parts[2];const url = providers[providerName][mapName][mapType];options.subdomains = providers[providerName].Subdomains;L.TileLayer.prototype.initialize.call(this, url, options);}});const imgParam = "&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&FORMAT=tiles";const ciaParam = "&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cia&STYLE=default&TILEMATRIXSET=w&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&FORMAT=tiles";const tk = "自己的token"L.TileLayer.ChinaProvider.providers = {TianDiTu: {Satellite: {Map: "https://t{s}.tianditu.gov.cn/img_w/wmts?tk=" + tk + imgParam,Annotation: "https://t{s}.tianditu.gov.cn/cia_w/wmts?tk=" + tk + ciaParam},Subdomains: ['0', '1', '2', '3', '4', '5', '6', '7']},};L.tileLayer.chinaProvider = function (type, options) {return new L.TileLayer.ChinaProvider(type, options);};/*** 天地图内容*/const imga = L.tileLayer.chinaProvider('TianDiTu.Satellite.Annotation', {maxZoom: 26,minZoom: 1,});imga.bringToFront()//题图titleLayerconst ia = L.layerGroup([imga])let mapif (zoomSnap !== undefined) {map = L.map(containerId, {editable: false,center: centralPoint,editOptions: {},zoom: zoom,layers: [ia],zoomSnap: zoomSnap,closePopupOnClick: false,crs: L.CRS.EPSG3857,attributionControl: false,});} else {map = L.map(containerId, {editable: true,center: centralPoint,editOptions: {},zoom: zoom,layers: [ia],zoomControl: false,closePopupOnClick: false,crs: L.CRS.EPSG3857,// 去掉比列尺attributionControl: false,});} ia.setZIndex(100000)return {"map": map,};
}

组件内部使用:

<template><div class="map-container" id="mapContainer" />
</template><script setup>
import initMap from "@/utils/leaflet/j-map";
const initMapLayers = () => {const mapResult = initMap(`mapContainer`, [...epicentreLocation.value], 6, undefined);map.value = mapResult.map;
}
</script>
<style lang="less" scoped>
.map-container {width: 100%;height: 100%;background: linear-gradient(rgba(0, 52, 204, 1), rgba(0, 52, 204, 1)) center/80% 80%, lightblue;}
</style>

最后说明:经验丰富,诚接各种项目开发。非诚勿扰哦!!千万别乱加!!!联系方式:如下
在这里插入图片描述

版权声明:

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

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