[vue2项目]vue2+supermap[mapboxgl]+天地图之地图的基础操作(画线+自定义打点)

二、地图的基础操作

1、画线

案例(1)

javascript 复制代码
 this.map.on("load", () => {
        let geometryLine = {
          type: "Feature",
          geometry: {
            // 定义类型
            type: "LineString",
            coordinates: [
              [113.39793764, 34.05675322],
              [113.35187554, 32.4392251],
              [112.47685179, 31.89344325],
              [112.29263185, 30.75257895],
              [112.43079033, 30.15709126],
              [113.9505599, 29.75808719],
              [114.45714743, 29.23699965],
              [115.34044715, 28.22369668],
              [115.59740867, 27.5140793],
              [115.59740829, 27.45850126]
            ],
          },
        };
        this.map.addLayer({
          id: "river",
          type: "line",
          source: {
            type: "geojson",
            data: geometryLine,
          },
          layout: {
            "line-join": "round",
            "line-cap": "round",
          },
          paint: {
            "line-color": "red",
            "line-width": 5,
          },
        });
      });

案例 (2)绘制中国边界线

Datav

在Datav网站上获取中国边界线经纬度
在scr目录下创建util文件并把下载的json文件保存在该目录下,并改名为China.json

在组件中引入json文件

运行后

主要代码

javascript 复制代码
this.ChinaBorder.features[0].geometry.coordinates.forEach((item,index) => {
        let coordinates = [];
        item.forEach((item2) => {
          coordinates.push(item2);
        });
        // 处理中国边界经纬度数据
        this.map.on("load", () => {
          let geometryLine = {
            type: "Feature",
            geometry: {
              // 定义类型
              type: "LineString",
              coordinates: coordinates,
            },
          };
          this.map.addLayer({
            id: "border"+index,
            type: "line",
            source: {
              type: "geojson",
              data: geometryLine,
            },
            layout: {
              "line-join": "round",
              "line-cap": "round",
            },
            paint: {
              "line-color": "red",
              "line-width": 10,
            },
          });
        });
      });

2、自定义打点(图片+文字)

javascript 复制代码
const el = document.createElement("div");
      el.className = "icon_box";
      el.style.width = `100px`;
      el.style.height = `100px`;
      el.style.backgroundSize = "100%";
      el.innerHTML = `
      <div>
        <div class='icon' style='width:30px;height:30px;'></div>
          <span class='title'>文字说明</span>
      </div>`;
      new mapboxgl.Marker(el)
        .setLngLat([118.0626924, 26.71411572])
        .addTo(this.map);
css 复制代码
/deep/.icon_box {
    // position: relative;
    z-index: 8;
    width: 30px !important;
    height: 30px !important;
    font-size: 12px;
    color: #fff;
    // 图片
    .icon {
      width: 30px !important;
      height: 30px !important;
      z-index: 5;
      background-image: url("../../static/location.png");
      background-size: cover;
    }
    // 文字说明样式
    .title{
      position: absolute;
      bottom: 35px;
      white-space: nowrap;
      left:20px;
      background: #212f7f;
      color: #94e9ff;
      padding: 3px 8px 2px 8px;
      border-radius: 5px;
      box-shadow: 0 0 5px rgba(0, 0, 0, 0.3), 0 0 5px rgba(0, 0, 0, 0.3);
    }
  }
相关推荐
FRing038 小时前
vue2干货总结2(v-bind、v-for、v-model、指令修饰符、v-bind操作class和style)
开发语言·前端·vue.js·vue2
徐小夕11 小时前
深度剖析 30k star 的编码 Agent DeepSeek-Reasonix:长会话 Token 成本直降 80%
前端·vue.js·github
ruanno_top14 小时前
1 万行表格只渲染 18 行:虚拟滚动让我放弃了三个功能
vue.js
仨孩子的爹14 小时前
套打总偏两毫米:预印刷单据的校准方法和三个误区
vue.js
徐小夕14 小时前
JitKnow知识库4.8发布:本体+知识图谱检索引擎,让AI回答不再“一本正经地胡说八道”
前端·vue.js·github
tryCbest15 小时前
Vue3——第 11 篇:组合式 API(上)——setup 与响应式 API 深度解析
前端·javascript·vue.js
tryCbest16 小时前
Vue3——第 13 篇:计算属性与侦听器——computed 与 watch 的进化
前端·javascript·vue.js
闲来没事抠鼻屎16 小时前
标签打印 8 个尺寸公式:小票、价签、吊牌、面单通用
vue.js
时间的情敌17 小时前
Vue3 + Cesium 实战:本地上传 GLB 模型、模型上叠加 GLB、点击控制透明度
前端·vue.js·cesium
小蒜学长19 小时前
基于SpringBoot+Vue的云南地区扶贫助农系统的设计与实现(代码+数据库+LW)
java·vue.js·spring boot·后端·乡村振兴