记录 vue -amap 开发 高德地图 调用原生sdk 多边形得新增和编辑

记录 vue -amap 开发 高德地图 调用原生sdk 多边形得新增和编辑

import VueAMap from "vue-amap";
 Vue.use(VueAMap);
VueAMap.initAMapApiLoader({
  // 高德的key
  key: "859c0d27c83bc725a37fe2b7b2b4f3c2",
  // 插件集合
  plugin: [
    "AMap.Autocomplete",
    "AMap.PlaceSearch",
    "AMap.Scale",
    "AMap.OverView",
    "AMap.ToolBar",
    "AMap.MapType",
    "AMap.PolyEditor",
    "AMap.CircleEditor",
    "AMap.Geocoder",
    "AMap.MouseTool",
  ],
  // 高德 sdk 版本,默认为 1.4.4
  v: "1.4.4",
});

父组件代码:

<TestAmap
      :visible.sync="diglogVisibleDraw"
      @sendMsg="sendMsg"
      :amapCompData="amapCompData"
    />
edit(row) {
      this.amapCompData.editRow = row;
      this.amapCompData.type = "edit";
      this.diglogVisibleDraw = true;
    },
    sendMsg(msg) {
      this.mapObj = {
        state: true,
        name: "配送区域",
        business_start_time_string: this.serveTime[0]
          ? this.serveTime[0]
          : "08:00",
        business_end_time_string: this.serveTime[1]
          ? this.serveTime[1]
          : "22:00",
        delivery_extend_time_int: 1,
        fee: 0,
        path: msg,
      };
      this.polygons.push(this.mapObj);
      this.mapData = this.polygons;
      this.diglogVisibleDraw = false;
    },

子组件代码

 <div class="map" v-show="visible">
   <div class="main">
     <div class="top">
        <div>
          <div v-if="amapCompData.type == 'add'">
            <el-button type="primary" @click="startDraw()" v-if="!flag"
              >开始绘制</el-button
            >
            <template v-else>
              <el-button type="primary" @click="addDrawMap()">添加</el-button>
              <el-button type="primary" @click="cancelDrawMap()"
                >取消</el-button
              >
            </template>
          </div>
          <div v-else>
            <el-button type="primary" @click="editDrawMap()">编辑</el-button>
            <el-button type="primary" @click="editDrawMapASave()"
              >编辑保存</el-button
            >
          </div>
        </div>
        <i class="el-icon-close digclose" @click="close"></i>
      </div>
      <div id="container"></div>
    </div>
  </div>
import { lazyAMapApiLoaderInstance } from "vue-amap";
export default {
  props: ["visible", "amapCompData"],
  data() {
    return {
      map: {},
      mouseTool: {},
      polygon: [],
      flag: false,
      polyEditor: {},
      ployStyle: {
        fillColor: "#FF0000",
        strokeColor: "#FF0000",
        strokeWeight: 2,
        strokeOpacity: 0.8,
        fillOpacity: 0.5,
      },
    };
  },
  watch: {
    visible() {
      if (this.amapCompData.type == "add") {
        this.initMap();
      } else {
        this.initEdit();
      }
    },
  },
  mounted() {},
  methods: {
  
    initOption() {
      this.flag = false;
      this.polygon = [];
      this.map = {};
      this.mouseTool = {};
    },
  
    initMap() {
      this.initOption();
      let self = this;
      this.$nextTick(function() {
        lazyAMapApiLoaderInstance.load().then(() => {
          self.map = new AMap.Map("container", {
            center: this.amapCompData.center,
            zoom: 14,
          });
          var marker = new AMap.Marker({
            position: this.amapCompData.center,
            title: "门店位置",
          });
          self.map.add(marker);
        });
      });
    },

    initEdit() {
      this.flag = false;
      this.map.clearMap();
      this.polygon = [];
      let self = this;
      let path = this.amapCompData.editRow.path;
      let polygon = new AMap.Polygon({
        path,
        ...this.ployStyle,
      });
      this.map.add(polygon);
      this.map.setFitView();

      this.polyEditor = new AMap.PolyEditor(this.map, polygon);
      this.polyEditor.on("end", function(event) {
        self.polygon.push(event.target.getPath());
      });
    },
    editDrawMap() {
      this.polyEditor.open();
      this.flag = true;
    },
    editDrawMapASave() {
      this.polyEditor.close();
      this.$emit("update:visible", false);
    },
 
    startDraw() {
      let self = this;
      self.flag = true;
      self.mouseTool = new AMap.MouseTool(self.map);
      var distanceTool = new AMap.MouseTool(self.map);
      distanceTool.rule();
      self.mouseTool.on("draw", function(e) {
        if (self.polygon.length == 0) {
          self.polygon.push(e.obj.getPath());
        } else {
          self.clear();
          self.$message.error("每次只能添加一个,请重新绘制");
        }
      });
      this.mouseTool.polygon(this.ployStyle);
    },
    cancelDrawMap() {
      this.clear();
      this.flag = false;
    },
    clear() {
      this.map.clearMap();
      this.polygon = [];
    },
    addDrawMap() {
      if (this.polygon.length == 0) {
        this.$message.error("未添加区域");
      } else {
        sessionStorage.setItem("mapData", JSON.stringify(this.polygon));
        this.$emit("sendMsg", this.polygon);
      }
    },
    close() {
      this.$emit("update:visible", false);
    },
  },
};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue-amap 是一个基于 Vue.js高德地图组件库,支持在线地图和离线地图。实现高德离线地图,需要先下载离线地图数据,在 Vue-amap 中引入并配置离线地图数据即可。 以下是实现步骤: 1. 下载离线地图数据:在高德官网下载离线地图数据,下载后解压缩得到离线地图数据文件夹。 2. 在 Vue 项目中安装 vue-amap: ``` npm install vue-amap --save ``` 3. 在 main.js 中引入和配置 vue-amap: ``` import VueAMap from 'vue-amap'; Vue.use(VueAMap); VueAMap.initAMapApiLoader({ key: 'your amap key', plugin: ['AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.Geolocation'], // 默认高德 sdk 版本为 1.4.4 v: '1.4.4', // 配置离线地图数据路径,注意路径要使用绝对路径 uiVersion: '1.0', map: { // 配置离线地图数据路径,注意路径要使用绝对路径 mapStyle: 'amap://styles/whitesmoke', features: ['bg', 'road', 'building'], customMapStyle: { 'styleJson': [ { 'featureType': 'water', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'land', 'elementType': 'all', 'stylers': { 'color': '#f3f3f3' } }, { 'featureType': 'railway', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'highway', 'elementType': 'all', 'stylers': { 'color': '#fdfdfd' } }, { 'featureType': 'highway', 'elementType': 'labels', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'arterial', 'elementType': 'geometry', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'arterial', 'elementType': 'geometry.fill', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'poi', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'green', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'subway', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'manmade', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'local', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'arterial', 'elementType': 'labels', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'boundary', 'elementType': 'all', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'building', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'label', 'elementType': 'labels.text.fill', 'stylers': { 'color': '#999999' } } ] }, // 配置离线地图数据路径,注意路径要使用绝对路径 zooms: [3, 20], viewMode: '3D', expandZoomRange: true }, // 配置离线地图数据路径,注意路径要使用绝对路径 filePath: '/path/to/offline/map/data' }); ``` 注意,需要将 filePath 设置为离线地图数据文件夹的绝对路径。 4. 在组件中使用 Vue-amap: ``` <template> <el-amap :zoom="13" :center="center"> <el-amap-marker :position="center"></el-amap-marker> </el-amap> </template> <script> export default { data () { return { center: [116.397428, 39.90923] }; } }; </script> ``` 在 el-amap 中设置 zoom 和 center 即可显示地图。el-amap-marker 可以设置标记点。 至此,就完成了 Vue-amap 实现高德离线地图的步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

web_Hsir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值