vue2+天地图

天地图在vue2中的使用

1. 在index.html中引入,当前是把天地图放在了服务器,这样加载会快
<script src="http://xxxxxxxx/tianditu/tianditu.js"></script>
2. 在vue.config.js中设置全局T
3. 在页面中使用:实现效果:左侧查询指定网关后,可在天地图中显示信息,打点是根据接口返回的经纬度赋值
 configureWebpack: {
    externals: {
      T: "T", // 天地图全局名称
      // 项目内使用时的组件名称: 某外部组件对外暴露的名称
    },
 }
3. 在页面中使用(完整代码)
<template>
  <div class="equipmentMap">
    <el-input
      class="seachInput"
      placeholder="请输入网关名称或SN"
      suffix-icon="el-icon-search"
      @keyup.enter.native="getGatewayData"
      v-model="snOrName"
    >
    </el-input>
    <div class="mapBox" v-if="gatewayData.length">
      <div
        class="mapBox_frame"
        v-for="(item, index) in gatewayData"
        :key="index"
        @click="goIcon(item)"
      >
        <div class="name ellipsis" :title="item.name">
          网关名称:{{ item.name }}
        </div>
        <div class="other ellipsis">分组:{{ item.group.toString() }}</div>
        <div class="other ellipsis">SN:{{ item.sn }}</div>
      </div>
    </div>
    <div id="mapDiv"></div>
  </div>
</template>
<script>
import { list } from "@/api/deviceManagement/gateway";
export default {
  name: "EquipmentOverview",
  data() {
    return {
      gatewayData: [],
      snOrName: "",
      siteArr: [],
      map: null,
    };
  },
  filters: {},
  mounted() {
    this.getGatewayData();
    this.$nextTick(() => {
      this.loadMap();
    });
  },
  methods: {
    getGatewayData() {
      let params = {
        pageNum: 1,
        pageSize: 5000,
        snOrName: this.snOrName,
      };
      list(params).then((response) => {
        if (response.rows) {
          response.rows.map((v) => {
            v.group = [];
            v.deptDtoList.map((m) => {
              v.group.push(m.deptName);
            });
            this.siteArr.push({
              longitude: v.position ? v.position.split(",")[0] : "",
              latitude: v.position ? v.position.split(",")[1] : "",
              name: v.name,
              group: v.group,
              sn: v.sn,
            });
          });
          this.gatewayData = response.rows;
          if (this.siteArr.length > 0) {
            this.setPoint();
          }
        } else {
          this.gatewayData = [];
        }
      });
    },
    loadMap() {
      let that = this;
      let zoom = 16;
      //初始化地图对象
      this.map = new T.Map("mapDiv");
      //设置显示地图的中心点和级别
      let lng = 116.849;
      let lat = 28.202988;
      if (that.lnglat) {
        lng = that.lnglat.split(",")[0];
        lat = that.lnglat.split(",")[1];
      }
      this.map.centerAndZoom(new T.LngLat(lng, lat), zoom);
      //创建缩放平移控件对象
      let control = new T.Control.Zoom();
      //添加缩放平移控件
      this.map.addControl(control);
      this.map.setMinZoom(10);
      this.map.setMaxZoom(17);
    },
    setPoint() {
      const site = this.siteArr;
      site.forEach((item) => {
        // 创建标注对象
        const marker = new T.Marker(
          new T.LngLat(item.longitude, item.latitude),
          {
            icon: new T.Icon({
              iconUrl: require("./marker-icon.png"), // 引入自定义图标位置
              iconSize: new T.Point(18, 26), // 图标大小
            }),
          }
        );
        item.marker = marker;
        // 向地图上添加标注
        this.map.addOverLay(marker);
        // 点位的点击事件,展示弹窗
        this.addClickHandler(item, marker);
      });
    },
    // 点位弹窗
    addClickHandler(content, marker) {
      var T = window.T;
      const that = this;
      marker.addEventListener("click", function (e) {
        var markerInfoWin = new T.InfoWindow(); // 创建信息窗口对象
        // 弹窗模版
        const sContent = `<div class="module-title" style="color: black;font-size: 14px;">网关名称:${content.name}</div>
       <div class="module-title" style="color: black;font-size: 14px;">分组:${content.group}</div>
       <div class="module-title" style="color: black;font-size: 14px;">SN:${content.sn}</div>
      `;
        // 相对于点偏移的位置
        markerInfoWin.setContent(sContent, { offset: new T.Point(0, -30) });
        that.map.openInfoWindow(markerInfoWin, e.lnglat); // 开启信息窗口
      });
    },
    goIcon(item) {
      if (item.position) {
        setTimeout(() => {
          this.map.panTo(
            new T.LngLat(
              item.position ? item.position.split(",")[0] : "",
              item.position ? item.position.split(",")[1] : ""
            ),
            40
          );
          this.siteArr.map((v) => {
            if (v.sn == item.sn) {
              var markerInfoWin = new T.InfoWindow(); // 创建信息窗口对象
              // 弹窗模版
              const sContent = `<div class="module-title" style="color: black;font-size: 14px;">网关名称:${v.name}</div>
       <div class="module-title" style="color: black;font-size: 14px;">分组:${v.group}</div>
       <div class="module-title" style="color: black;font-size: 14px;">SN:${v.sn}</div>
      `;
              // 相对于点偏移的位置
              markerInfoWin.setContent(sContent, {
                offset: new T.Point(0, -30),
              });
              this.map.openInfoWindow(
                markerInfoWin,
                new T.LngLat(
                  v.longitude ? v.longitude : "",
                  v.latitude ? v.latitude : ""
                )
              ); // 开启信息窗口
            }
          });
        }, 1000);
      } else {
        this.map.closeInfoWindow()
        return;
      }
    },
  },
};
</script>
<style lang="scss" scoped>
#mapDiv {
  width: 100%;
  height: 94vh;
}
.seachInput {
  position: absolute;
  z-index: 9999;
  margin: 0.6% 0px 0px 3%;
  width: 300px;
}
.mapBox {
  border: 1px solid #eaecf0;
  background-color: white;
  height: 303px;
  overflow: auto;
  width: 300px;
  position: absolute;
  z-index: 9999;
  margin: 2.7% 0px 0px 3%;
}
.mapBox_frame {
  height: 100px;
  width: 294px;
  background-color: white;
  border-bottom: 1px solid #efeded;
  padding: 14px;
  line-height: 24px;
  .name {
    font-weight: 400;
    font-size: 16px;
    color: #333333;
    cursor: pointer;
  }
  .ellipsis {
    width: 240px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1; /*3表示只显示3行*/
    -webkit-box-orient: vertical;
  }
  .other {
    font-weight: 400;
    font-size: 14px;
    color: #666666;
    cursor: pointer;
  }
}
.mapBox_frame:hover {
  background-color: rgb(221, 239, 250); /* 鼠标滑过时的背景颜色 */
  color: white; /* 鼠标滑过时的文本颜色 */
}
::-webkit-scrollbar {
  width: 4px;
  height: 6px;
}
::-webkit-scrollbar-thumb {
  border-radius: 10px;
  box-shadow: inset 0 0 5px rgba(97, 184, 179, 0.1);
  background: rgba(144, 147, 153, 0.3);
}
.easy-scrollbar__wrap::-webkit-scrollbar-track {
  box-shadow: inset 0 0 5px rgba(87, 175, 187, 0.1);
  border-radius: 20px;
  background: #ededed;
}
/deep/ .tdt-control-container {
  display: none !important;
}
</style>

在这里插入图片描述

实现需求:
选择位置,打点,获取经纬度,也可在搜索框中搜索指定位置进行打点

在这里插入图片描述
在这里插入图片描述

<CompDeviceMap
      v-if="mapFlag"
      :locaData="{ location: gatewayForm.address, lnglat }"
      @on-change-mapFlag="changemapFlag"
      @choose-map="chooseMap"
    ></CompDeviceMap>
data(){
lnglat: "", //经纬度
gatewayForm: {
        address:""
      },
}

 methods: {
    chooseMap(val) {
      this.gatewayForm.address = val.location;
      this.lnglat = val.lnglat;
    },
    changemapFlag(val) {
      this.mapFlag = val;
    },
}

子组件CompDeviceMap.vue

<template>
  <div>
    <el-dialog
      :visible.sync="mapFlag_"
      title="选择位置"
      append-to-body
      :close-on-click-modal="false"
    >
      <div>
        <div class="content-div">
          <div class="input-div">
            <span style="padding-top: 5px">当前位置:</span>
            <div>
              <div :class="redBox ? 'redBox' : ''">
                <el-input
                  v-model="location"
                  @input="inputDataChange($event)"
                  @keyup.enter.native="searchLocation()"
                ></el-input>
              </div>
              <div v-if="redBox" class="redText">
                地址的长度在1到100个字符之间
              </div>
            </div>
          </div>
          <el-button type="primary" @click="onSubmit" icon="el-icon-check"
            >确定</el-button
          >
        </div>
        <div id="mapDiv"></div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    mapFlag: {
      type: Boolean,
      default() {
        return true;
      },
    },
    locaData: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      num: 0,
      location: "",
      lnglat: "",
      redBox: false,
      map: null,
    };
  },
  computed: {
    mapFlag_: {
      get() {
        return this.mapFlag;
      },
      set(val) {
        this.$emit("on-change-mapFlag", val);
      },
    },
  },
  methods: {
    loadMap() {
      let that = this;
      let zoom = 16;
      //初始化地图对象
      this.map = new T.Map("mapDiv");
      //设置显示地图的中心点和级别
      let lng = 116.85;
      let lat = 28.202988;
      if (that.lnglat) {
        lng = that.lnglat.split(",")[0];
        lat = that.lnglat.split(",")[1];
      }
      this.map.centerAndZoom(new T.LngLat(lng, lat), zoom);
      //创建缩放平移控件对象
      let control = new T.Control.Zoom();
      //添加缩放平移控件
      this.map.addControl(control);
      this.map.setMinZoom(12);
      this.map.setMaxZoom(17);
      // 根据经纬度进行地图标记
      if (that.lnglat) {
        var marker = new T.Marker(new T.LngLat(lng, lat));
        this.map.addOverLay(marker);
      }
      this.map.addEventListener("click", function (e) {
        //创建标注对象
        that.map.clearOverLays();
        let geocoder = new T.Geocoder();
        geocoder.getLocation(e.lnglat, (res) => {
          var marker = new T.Marker(
            new T.LngLat(e.lnglat.getLng(), e.lnglat.getLat())
          );
          that.map.addOverLay(marker);
          that.searchResult(res);
          that.lnglat = `${e.lnglat.getLng()},${e.lnglat.getLat()}`;
        });
      });
    },
    searchResult(res) {
      if (res.getStatus() == 0) {
        this.location = res.getAddress();
      } else {
        this.location = "";
      }
    },
    onSubmit() {
      if (this.num == 0 && !this.location) {
        this.inputDataChange();
        this.num++;
      } else {
        if (this.location && this.location.length < 101) {
          if (!this.location) {
            this.$message({
              message: "请选择定位地址",
              type: "warning",
            });
            return;
          }
          this.num = 0;
          this.mapFlag_ = false;
          this.$emit("choose-map", {
            location: this.location,
            lnglat: this.lnglat,
          });
        }
      }
    },
    inputDataChange() {
      if (!this.location || this.location.length > 100) {
        this.redBox = true;
      } else {
        this.redBox = false;
      }
    },
    searchLocation() {
      this.map.clearOverLays();
      let geocoder = new T.Geocoder();
      let that = this;
      geocoder.getPoint(this.location, function searchSite(result) {
        that.map.panTo(result.getLocationPoint(), 16);
        //创建标注对象
        var marker = new T.Marker(result.getLocationPoint());
        //向地图上添加标注
        that.map.addOverLay(marker);
        that.lnglat = `${result.getLocationPoint().lat},${result.getLocationPoint().lng}`;
      });
    },
  },
  mounted() {
    if (this.locaData) {
      this.location = this.locaData.location;
      this.lnglat = this.locaData.lnglat;
    }
    this.$nextTick(() => {
      this.loadMap();
    });
  },
};
</script>
<style lang="scss" scoped>
.content-div {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  .input-div {
    span {
      width: 77px;
    }
    display: flex;
    .redText {
      color: #f56c6c;
      font-size: 12px;
    }
    // align-items: center;
  }
}
.redBox {
  /deep/.el-input__inner {
    border: 1px solid #f56c6c !important;
  }
}

#mapDiv {
  width: 100%;
  height: 400px;
}
/deep/ .tdt-control-container {
  display: none !important;
}
</style>
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue Leaflet 是一种结合了Vue框架和Leaflet库的前端技术,用于展示和操作地图地图是一种具有高清影像和矢量数据的地图服务,提供了丰富的地理信息资源和功能,如地图展示、地图操作、定位导航等。 Vue Leaflet 可以通过调用地图的API接口,获取并展示地图的各类地理信息。通过Vue的组件化开发方式,可以方便地在Vue项目中使用这些地理信息,实现自定义的地图功能。例如,在Vue Leaflet 中可以实现地图、标记点、线段、面等地理要素的显示和编辑。 Vue Leaflet 提供了一套方便易用的API和组件,可以轻松地在Vue项目中集成和使用地图。比如,可以使用Vue Leaflet 提供的地图组件将地图展示在网页中,可以使用它提供的标记点组件在地图上添加标记,可以使用它提供的工具条组件进行地图的操作和导航等。 使用Vue Leaflet 可以有效地提高开发效率和用户体验。通过其简洁的API和灵活的组件,开发人员可以快速地实现各种地图需求,如显示地图、标记地点、展示线段等。并且,Vue Leaflet 结合了Vue框架的优势,可以更好地组织和管理地图相关的逻辑代码,使开发工作更加方便和高效。 总之,Vue Leaflet 是一种方便、灵活和高效的前端技术,用于展示和操作地图。它通过结合Vue框架和Leaflet库,提供了一套方便易用的API和组件,帮助开发人员快速实现各种地图需求,提高开发效率和用户体验。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值