当前位置: 首页 > article >正文

leaflet(一)初始化地图

Leaflet 与天地图结合使用,可以通过天地图提供的 API 获取地图瓦片,并在 Leaflet 地图上显示。

1. 安装依赖

首先,确保你已经安装了 Leaflet 和 Vue:

npm install leaflet
npm install vue-leaflet
npm install leaflet.tilelayer.colorfilter   //添加带有颜色过滤器的瓦片层

2. 引入 Leaflet 和样式

在你的 Vue 组件中引入 Leaflet 和其样式文件:

<template>
  <div
    id="mapDiv"
    class="map"
    style="width: 100%; height: 100%; background-color: rgba(15, 44, 80, 0.9) !important;"
  ></div>
</template>

<script>
import L from "leaflet";
import "leaflet.tilelayer.colorfilter";
import "leaflet/dist/leaflet.css";
let atopMap = null;
export default {
    mounted() {
        this.initMap();
        this.addTianDiTuLayer();
    },
    methods: {
        //初始化地图
        initMap() {
            atopMap = L.map("mapDiv", {
            //参考坐标系
            crs: L.CRS.EPSG3857,
            // 中心点(这里是北京)
            center: [39.89945, 116.40769],
            //最小显示等级
            minZoom: 0,
            //最大显示等级
            maxZoom: 18,
            //当前显示等级
            zoom: 13,
            zoomControl: false,
            contextmenu: true,
            //不添加属性说明控件
            attributionControl: false,
          });

          //定义一个比例尺控件
          const scaleControl = L.control.scale();
          //将比例尺控件加载到地图容器中
          atopMap.addControl(scaleControl);
        },
        //增加天地图底图图层
        addTianDiTuLayer() {
            // 矢量图+注记
            // let mapTypes = ['vec_c', 'cva_c'];
            let mapTypes = ["vec_w", "cva_w"];
            let layers = [];
            for (let i = 0, len = mapTypes.length; i < len; i++) {
                let tdtUrl = `http://t0.tianditu.gov.cn/DataServer?T=${mapTypes[i]}&x={x}&y={y}&l={z}&tk=${YOUR_API_KEY}`;
                let layer = L.tileLayer.colorFilter(tdtUrl, {
                    filter: [
                        "grayscale:100%",
                        "invert:100%",
                        "brightness:60%",
                        "hue:220deg",
                        "saturate:54%",
                        "opacity: 50%",
                    ],
                    zoomOffset: 0, // 注意如果是 web 墨卡托投影(EPSG:3857) 请改为 0
                    noWrap: true,
                    bounds: [
                        [-90, -180],
                        [90, 180],
                    ],
                });
                layers.push(layer);
            }
            // LayerGroup 对象
            L.layerGroup(layers).addTo(atopMap);
        },
    }
}
</script>

<style lang="scss" scoped>
.tdt-tile-pane {
  .tdt-layer:first-child + .tdt-layer {
    .tdt-tile-loaded {
      -webkit-filter: grayscale(100%) invert(100%);
      opacity: 0.5 !important;
    }
  }
}

::v-deep .icon-label {
  color: #fff;
  width: 70px;
  text-align: center;
  font-size: 14px !important;
}

::v-deep .tdt-control-container {
  display: none !important;
}

.map {
  background-color: rgba(15, 44, 80, 0.9) !important;
}
</style>


http://www.kler.cn/news/354554.html

相关文章:

  • Mybatis--简略2
  • 树莓派4B-用串口读取JY901S陀螺仪数据
  • JNI是什么
  • vue综合指南(六)
  • 自动化工具:Ansible
  • 基于腾讯云的AI视频课程制作工具
  • 如何在Python中编写自定义上下文管理器?
  • 【AIGC】让AI像人一样思考和使用工具,reAct机制详解
  • 基于Springboot+Vue的农业收成管理系统(含源码数据库)
  • SpringBoot驱动的高校学科竞赛平台开发指南
  • HarmonyOS NEXT开发 ArkTS自定义组件
  • OPENSSL-2023/10/31学习记录(单向散列函数)
  • 【网络安全】-web安全-基础知识梳理
  • Junit单元测试时提示:Method should have no parameters
  • qiankun 应用之间数据传递
  • linux 开发机与测试机建立 ssh 隧道
  • Vue3的Composition组合式API(computed计算属性、watch监视属性、watchEffect函数)
  • TDengine 3.3.3.0 发布:新增 MySQL 函数与 MongoDB 数据源支持
  • 鸿蒙网络编程系列7-TLS安全数据传输单向认证示例
  • c# FrozenDictionary