<template>
  <div class="home_div">
    <div id="container"></div>
    <div class="input-card" style="width: 150px">
      <el-button
        class="btn"
        @click="drawPolygon"
        style="margin-bottom: 5px"
        v-if="this.path.length == 0"
      >
        绘制区域
      </el-button>
      <el-button
        class="btn"
        @click="handleopen"
        style="margin-bottom: 5px"
        v-if="this.path.length > 0"
      >
        开始编辑
      </el-button>
      <el-button
        class="btn"
        @click="handleClose"
        v-if="this.path.length > 0"
        style="margin-bottom: 5px"
      >
        保&nbsp;&nbsp;&nbsp;&nbsp;存
      </el-button>
      <el-button class="btn" @click="handleClear" v-if="this.path.length > 0">
        重&nbsp;&nbsp;&nbsp;&nbsp;绘
      </el-button>
    </div>
  </div>
</template>
<script>
import AMapLoader from "@amap/amap-jsapi-loader";
window._AMapSecurityConfig = {
  securityJsCode: "", //在这里填写你的安全密钥
};
export default {
  name: "MapContainer",
  props: {
    path: {
      type: Array,
      default: () => [],
    },
    name: {
      type: String,
      default: () => "",
    },
  },
  data() {
    return {
      //map:null,
      mouseTool: null,
      polygon: null,
      polyEditor: null,
      pathLng: [],
      placeSearch: null,
      options: [], // 可选数据列表,详见组件文档
      selectedOptions: [], // 当前已选数据
      schoolForm: {},
    };
  },
  computed: {
    schoolList() {
      return this.$store.state.SchoolList;
    },
  },
  created() {},
  mounted() {
    this.initAMap();
  },
  methods: {
    initAMap() {
      AMapLoader.load({
        key: "", //设置您的key
        version: "2.0",
        plugins: [
          // "AMap.ToolBar",
          "AMap.Scale",
          "AMap.Geolocation",
          // "AMap.ControlBar",
          "AMap.MouseTool",
          "AMap.PolygonEditor",
          "AMap.PlaceSearch",
        ],
        AMapUI: {
          version: "1.1",
          plugins: [],
        },
        Loca: {
          version: "2.0",
        },
      })
        .then((AMap) => {
          this.map = new AMap.Map("container", {
            viewMode: "3D",
            zoom: 15,
            zooms: [2, 22],
          });
          this.map.addControl(new AMap.Scale());
          this.map.addControl(
            new AMap.Geolocation({
              enableHighAccuracy: true, //是否使用高精度定位,默认:true
              timeout: 10000, //超过10秒后停止定位,默认:5s
              position: "RB", //定位按钮的停靠位置
              offset: [10, 20], //定位按钮与设置的停靠位置的偏移量,默认:[10, 20]
              zoomToAccuracy: true, //定位成功后是否自动调整地图视野到定位点
            })
          );

          this.mouseTool = new AMap.MouseTool(this.map);
          this.mouseTool.on("draw", (event) => {
            let data = event.obj.getOptions();
            //手动绘制时创建编辑器
            this.polygon = new AMap.Polygon({
              path: data.path,
              strokeColor: "#2695FF",
              strokeWeight: 6,
              strokeOpacity: 0.2,
              fillOpacity: 0.4,
              fillColor: "#1C92FF",
              zIndex: 50,
              bubble: true,
            });
            this.map.add([this.polygon]);
            this.polyEditor = new AMap.PolygonEditor(this.map, this.polygon);
            this.polyEditor.on("end", (event) => {
              this.$emit("mapEditEnd", event.target.getOptions());
            });
            this.$emit("mapDraw", data);
          });
          //矢量图形坐标数组大于0时创建编辑器
          if (this.path.length > 0) {
            this.polygon = new AMap.Polygon({
              path: this.path,
              strokeColor: "#2695FF",
              strokeWeight: 6,
              strokeOpacity: 0.2,
              fillOpacity: 0.4,
              fillColor: "#1C92FF",
              zIndex: 50,
              bubble: true,
            });
            this.map.add([this.polygon]);
            this.polyEditor = new AMap.PolygonEditor(this.map, this.polygon);
            this.polyEditor.on("end", (event) => {
              this.$emit("mapEditEnd", event.target.getOptions());
            });
            // this.handleSelectionChange(this.name);
          }
          this.placeSearch = new AMap.PlaceSearch({
            map: this.map,
            pageSize: 1,
            pageIndex: 1,
          });
        })
        .catch((e) => {
          console.log(e);
        });
    },
    drawPolygon() {
      this.mouseTool.polygon({
        strokeColor: "#2695FF",
        strokeWeight: 6,
        strokeOpacity: 0.2,
        fillColor: "#1C92FF",
        fillOpacity: 0.4,
        // 线样式还支持 'dashed'
        strokeStyle: "solid",
        // strokeStyle是dashed时有效
        // strokeDasharray: [30,10],
      });
    },
    handleSelectionChange(name) {
      const keyword = name;
      // 执行高德地图 POI 搜索,详见文档
      this.placeSearch.search(keyword, (status, result) => {
        if (status === "complete") {
          this.schoolForm = result.poiList.pois[0];
        }
      });
    },
    handleopen() {
      this.polyEditor.open();
    },
    handleClose() {
      this.polyEditor.close();
    },
    handleClear() {
      this.map.clearMap();
      this.$emit("mapDraw", []);
    },
  },
};
</script>
<style scoped>
.home_div {
  padding: 0px;
  margin: 0px;
  width: 100%;
  height: calc(100% - 50px);
  position: relative;
}

#container {
  padding: 0px;
  margin: 0px;
  width: 100%;
  height: 100%;
  position: absolute;
}
.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: fixed;
  top: 18rem;
  right: 5rem;
  -ms-flex: 1 1 auto;
  flex: 1 1 auto;
  padding: 0.75rem 1.25rem;
}
.btn {
  margin: 0;
}
</style>
//使用
  <MapContainerVue
  ref="MapContaine"
     @mapDraw="handleMapDraw"
     @mapEditEnd="handleMapEditEnd"
     :path="path"
     :name="MapName"
   ></MapContainerVue>
GitHub 加速计划 / vu / vue
80
16
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:4 个月前 )
73486cb5 * chore: fix link broken Signed-off-by: snoppy <michaleli@foxmail.com> * Update packages/template-compiler/README.md [skip ci] --------- Signed-off-by: snoppy <michaleli@foxmail.com> Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 6 个月前
e428d891 Updated Browser Compatibility reference. The previous currently returns HTTP 404. 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐