基于Vue框架开发的页面加载二维地图以及交互

一、在Vue项目中引入二维地图

1.切换到公司的仓库下载地图插件

npm config set registry http://nexus.toops.club/repository/npm-zui/

npm i tongmap-gl

2.在页面上加载二维地图

<template>
  <div style="width: 100%; height: 100%">
    <!-- 加载二维地图 -->
    <tong-gl-map
      :option="option"
      :region="region"
      :location="location"
      @maploadend="maploadend"
      ref="glmap"
    >
       <!-- 在地图上添加图层 -->
      <tong-gl-point-layer
        :option="pointOption"    
        :data="dateList"
      ></tong-gl-point-layer>
    </tong-gl-map>
  </div>
</template>

<script>
export default {
  name: "Map",
  components: { infoPoint },
  data() {
    return {
      location: {},
      //自定义配置图层中的图标
      pointOption: {
        parser: {
          lat: "y",
          lng: "x",
        },
        element: this.getElement(),  // 自定义展示的样式
        mode: "update",
        primaryKey: "id",
        elementOffset: [-6, 6],  //设置自定义元素的偏移
        autoOnView: true,
        zIndex: 5,
        type: "marker",
      },
      region: {
        type: "xyz",
        url: common.commonMapApi,  //配置的瓦片地址
      },
      option: {
        pitch: 125,  // 配置地图默认倾斜度
        backgroundColor: "rgba(0,25,46,0.9)",
        bounds: [   //设置最小边界
          [108.9212, 34.3703],
          [108.9499, 34.3835],
        ],
        maxBounds: [  //设置最大边界
          [104.875, 31.934],
          [112.731, 36.439],
        ],
      },
    };
  },
  computed: {
    dateList() {
      return this.$store.getters.dateList;   //地图上需要展示的点位数据
    },
  },
  methods: {
    /**
     * @name:设置地图指北操作
     * @msg:
     * @param {*}
     * @return {*}
     */
    setMapPointNorth() {
      this.map.setBearing(0);
    },
    /**
     * @name:设置地图不倾斜
     * @msg:
     * @param {*}
     * @return {*}
     */
    setMapnotTilted() {
      this.map.setPitch(0);
    },
    /**
     * @name:设置地图为倾斜
     * @msg:
     * @param {*}
     * @return {*}
     */
    setMaptTilted() {
      this.map.setPitch(125);  // 设置地图的倾斜度,最大值为125
    },
    /**
     * @name:地图放大操作
     * @msg:
     * @param {*}
     * @return {*}
     */
    mapEnlarge() {
      this.map.setZoom(this.map.getZoom() + 1);
    },
    /**
     * @name:地图缩小操作
     * @msg:
     * @param {*}
     * @return {*}
     */
    mapReduce() {
      this.map.setZoom(this.map.getZoom() - 1);
    },
    //地图加载完成事件,添加点位,圈层等操作都要在触发此事件后执行
    maploadend(evt) {
      //加载地图完成获得了地图对象
      this.map = evt.map;
      // 给map增加点击事件
      this.map.on("click", this.addSearchAlert.bind(this));
    },
     /**
     * @name:自定义展示的元素
     * @msg:
     * @param {*}
     * @return {*}
     */
    getElement() {
      return Vue.extend(infoPoint);
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
</style>

3.设置二维地图显示边界的方法

在这里插入图片描述
对应获取边界的地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值