高德地图实现多边形编辑器吸附功能

实现对地图的区域新建、编辑、样式修改、获取框选后点位合集,优化了官方案例的一些结构。

1. 效果

2. 安装插件 

npm i @amap/amap-jsapi-loader --save

3.代码

<template>
  <div class="campus-page">
    <div id="campusMap"></div>
    <div class="input-card" style="width: 120px">
      <button class="btn" @click="createPolygon()" style="margin-bottom: 5px">
        新建
      </button>
      <button class="btn" @click="startPolygon()" style="margin-bottom: 5px">
        开始编辑
      </button>
      <button class="btn" @click="closePolygon()">结束编辑</button>
    </div>
  </div>
</template>

<script>
import AMapLoader from "@amap/amap-jsapi-loader";
var polyEditor = "";
export default {
  data() {
    return {
      pathArr: [],
    };
  },
  mounted() {
    //DOM初始化完成进行地图初始化
    this.initMap();
  },
  methods: {
    async initMap() {
      await AMapLoader.load({
        key: "xxxxxxxxxxxxxxx", // 申请好的Web端开发者Key,首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: [
          "AMap.PolygonEditor",
          "AMap.Autocomplete",
          "AMap.PlaceSearch",
          "AMap.Scale",
          "AMap.OverView",
          "AMap.ToolBar",
          "AMap.MapType",
          "AMap.PolyEditor",
          "AMap.CircleEditor",
          "AMap.Geolocation",
          "AMap.Geocoder",
          "AMap.Polygon",
          "AMap.AMapUI",
        ], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      })
        .then((AMap) => {
          this.map = new AMap.Map("campusMap", {
            //设置地图容器id
            viewMode: "3D", //是否为3D地图模式
            zoom: 11, //初始化地图级别
            center: [113.00143, 28.192922], //初始化地图中心点位置
          });
          this.map.setFitView();
        })
        .catch((e) => {
          console.log(e);
        });
      this.initEditor();
    },
    initEditor() {
      polyEditor = new AMap.PolygonEditor(this.map);
      polyEditor.on("add",  (data) => {
        var polygon = data.target;
        polyEditor.addAdsorbPolygons(polygon);
        polygon.on("dblclick", () => {
          // 双击图层进行编辑
          polyEditor.setTarget(polygon);
          polyEditor.open();
        });
      });
      polyEditor.on("end",  (data) => {
        let obj = {
          key: data.target._opts.path,
          mapId: data.target._amap_id,
        };
        if (this.pathArr.length > 0) { // 判断是否有重复的点位,如果点位重复说明是修改之前的区域面积
          let isRepeat = this.pathArr.some((item, index) => {
            if (item.mapId === obj.mapId) {
              this.pathArr[index] = obj;
              return true;
            }
          });
          if (!isRepeat) {
            this.pathArr.push(obj);
          }
        } else { // 没有点位,直接添加
          this.pathArr.push(obj);
        }
        console.log(this.pathArr) // 最终的点位合集
      });
      polyEditor.open(); // 进入组件就可以编辑
      return polyEditor;
    },
    createPolygon() {
      //新建
      polyEditor.close();
      polyEditor.setTarget();
      polyEditor.open();
    },
    closePolygon() {
      // 关闭
      polyEditor.close();
    },
    startPolygon() {
      polyEditor.open();
    },
    campusOk() {
      polyEditor.close();
      console.log(this.pathArr.length);
      if (this.pathArr.length < 1) {
        return this.$message({
          message: "请划至少划分一个区域位置!",
          type: "warning",
        });
      }
      this.$emit("onPatharr", this.pathArr);// 在这里将点位合集传给父组件或者请求接口
    },
  },
};
</script>
<style></style>
<style scoped lang="scss">
#campusMap {
  padding: 0px;
  margin: 0px;
  width: 100%;
  height: 700px;
  flex: 7;
  position: relative;
}
.campus-page {
  position: relative;
}
.input-card {
  display: flex;
  flex-direction: column;
  min-width: 0;
  word-wrap: break-word;
  background-color: #fff;
  background-clip: border-box;
  border-radius: 0.25rem;
  width: 22rem;
  border-width: 0;
  border-radius: 0.4rem;
  box-shadow: 0 2px 6px 0 rgba(114, 124, 245, 0.5);
  position: absolute;
  bottom: 1rem;
  right: 1rem;
  -ms-flex: 1 1 auto;
  flex: 1 1 auto;
  padding: 0.75rem 1.25rem;
}
.btn {
  display: inline-block;
  font-weight: 400;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  border: 1px solid transparent;
  transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out,
    border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
  background-color: transparent;
  background-image: none;
  color: #25a5f7;
  border-color: #25a5f7;
  padding: 0.25rem 0.5rem;
  line-height: 1.5;
  border-radius: 1rem;
  -webkit-appearance: button;
  cursor: pointer;
}

.btn:hover {
  color: #fff;
  background-color: #25a5f7;
  border-color: #25a5f7;
}

.btn:hover {
  text-decoration: none;
}
</style>

4. 样式修改(不需要改样式的可以忽略)

    initEditor() {
      polyEditor = new AMap.PolygonEditor(this.map);
      polyEditor._opt.createOptions = { // 创建区域的样式
        fillColor: "#FF8D8D",
        fillOpacity: 0.3,
        strokeWeight: 3,
        strokeColor: "#FF8D8D",
      };
      polyEditor._opt.editOptions = {// 编辑区域的样式
        fillColor: "#FF8D8D",
        fillOpacity: 0.3,
        strokeWeight: 3,
        strokeColor: "#FF8D8D",
      };
      polyEditor.midControlPoint = { // 点位样式
        fillColor: "#FF8D8D",
        fillOpacity: 1,
        strokeWeight: 2,
        strokeColor: "#FF8D8D",
        bubble: false,
        clickable: true,
        cursor: "pointer",
      };
      polyEditor.controlPoint = {// 点位样式
        fillOpacity: 1,
        strokeWeight: 2,
        strokeColor: "#FF8D8D",
        bubble: false,
        clickable: true,
        cursor: "pointer",
      };
      polyEditor.on("add", (data) => {
        var polygon = data.target;
        polyEditor.addAdsorbPolygons(polygon);
        polygon.on("dblclick", () => {
          // 双击图层进行编辑
          polyEditor.setTarget(polygon);
          polyEditor.open();
        });
      });
      polyEditor.on("end", (data) => {
        let obj = {
          key: data.target._opts.path,
          mapId: data.target._amap_id,
        };
        if (this.pathArr.length > 0) {
          // 判断是否有重复的点位,如果点位重复说明是修改之前的区域面积
          let isRepeat = this.pathArr.some((item, index) => {
            if (item.mapId === obj.mapId) {
              this.pathArr[index] = obj;
              return true;
            }
          });
          if (!isRepeat) {
            this.pathArr.push(obj);
          }
        } else {
          // 没有点位,直接添加
          this.pathArr.push(obj);
        }
        console.log(this.pathArr); // 最终的点位合集
      });
      polyEditor.open(); // 进入组件就可以编辑
      return polyEditor;
    },

5. 修改颜色后的效果

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,您提到的问题是关于在 Vue 中绘制多边形并进行编辑的问题,对吗?那么,您可以使用高德地图 JavaScript API 中提供的绘制工具类来实现这一功能。具体步骤如下: 1. 引入高德地图 JavaScript API 和绘制工具类库: ```javascript <script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script> <script src="https://webapi.amap.com/ui/1.0/main.js"></script> ``` 2. 在 Vue 组件中创建地图容器,生成地图实例,并加载绘制工具类库: ```javascript <template> <div id="mapContainer"></div> </template> <script> export default { name: 'Map', mounted() { this.initMap(); }, methods: { initMap() { const map = new AMap.Map('mapContainer', { zoom: 10, center: [116.397428, 39.90923], }); AMap.plugin(['AMap.MouseTool'], () => { // 初始化鼠标绘制工具 const mouseTool = new AMap.MouseTool(map); // 监听draw事件,获取绘制的图形对象 AMap.event.addListener(mouseTool, 'draw', (e) => { console.log(e.obj); // e.obj为绘制出来的图形对象 // TODO: 将图形对象保存到state中,用于后续编辑 }); }); }, }, }; </script> ``` 3. 在绘制完成后,将绘制的图形对象保存到 state 中,以便进行后续编辑: ```javascript // 将绘制的图形对象保存到state中 // state.polygons为一个数组,保存所有已绘制的多边形对象 state.polygons.push(e.obj); ``` 4. 对已绘制的多边形对象进行编辑: ```javascript // 对已绘制的多边形对象进行编辑 const drawPolygon = (polygon) => { // 使用多边形编辑插件 const polygonEditor = new AMap.PolyEditor(map, polygon); // 开启编辑模式 polygonEditor.open(); // 监听编辑完成事件,保存编辑后的多边形对象到state中 AMap.event.addListener(polygonEditor, 'end', (e) => { console.log(e.target); // e.target为当前编辑的多边形对象 // TODO: 保存编辑后的多边形对象到state中 }); }; ``` 希望这个回答能够帮助您解决问题。如果您还有其他问题,可以继续向我提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值