高德地图区域选择、标点、搜索地址组件

高德地图区域选择、标点、搜索地址组件

<template>
  <div class="container">
    <div id="container" :style="{ height: height }" />
    <div class="input-card" v-show="type !== 3">
      <el-button @click="createPolygon()" v-show="cztype == 1 || showadd"
        >画定区域</el-button
      >
      <!-- <el-button @click="polyEditor.open()">指定坐标</el-button> -->
      <el-button @click="editClose">结束编辑</el-button>
    </div>
    <div class="serch">
      <div>
        <el-input
          id="mapInput"
          type="text"
          placeholder="请输入关键字(选定后搜索)"
          v-model="serach"
          onfocus='this.value=""'
        />
      </div>
    </div>
  </div>
</template>

<script>
import AMapLoader from "@amap/amap-jsapi-loader";
window._AMapSecurityConfig = {
  securityJsCode: "d94db7683b03635d1577e6591b3196c8" //你的秘钥
};
let polyEditor = "";
// import { defineProps } from "vue";
// const propsa = defineProps({
//   title: {
//     type: String,
//     default: "编辑"
//   },
//   pathList: {
//     type: [Object, String],
//     default: ""
//   }
// });
export default {
  name: "AreaMap",
  data() {
    return {
      map: null,
      // polyEditor: null,
      coordList: "",
      timer: "",
      form: {
        longitude: "",
        latitude: ""
      },
      pathList: [],
      serach: "",
      type: 0,
      showadd: false,
      mouseTool: null,
      overlays: [],
      auto: null,
      placeSearch: null
    };
  },
  props: {
    rowdata: {
      type: [Object, String],
      default: ""
    },
    cztype: {
      type: String,
      default: ""
    },
    height: {
      type: String,
      default: ""
    }
  }, // 接收传过来的数据

  mounted() {
    // axios
    //   .get(
    //     "https://restapi.amap.com/v3/geocode/geo?key=6abf1a62cc7f55d8741f0a14b25e23ac&address=" +
    //       this.pageList.start_address_province +
    //       this.pageList.start_address +
    //       "&city=" +
    //       this.pageList.start_city
    //   )
    //   .then(res => {
    //     if (res.data.status == "1") {
    //       let locationArr = res.data.geocodes[0].location;
    //       let location = locationArr.split(",");
    //       this.statrtlatlnt = location[0] + "," + location[1];
    //       this.somedistence();
    //     } else {
    //       this.$message.error("拾取失败,请重新核对地址");
    //     }
    //   });
    if (this.cztype !== 1 && this.rowdata.mapPoints !== null) {
      this.pathList = JSON.parse(this.rowdata.mapPoints);
      this.start();
      console.log("修改");
    } else if (this.cztype == 1) {
      this.echart();
      console.log("添加");
    } else {
      this.start();
      this.showadd = true;
    }
  },
  methods: {
    start() {
      this.timer = setInterval(this.echart, 1000); // 注意: 第一个参数为方法名的时候不要加括号;
    },
    async echart() {
      AMapLoader.reset();
      clearInterval(this.timer);
      await AMapLoader.load({
        key: "977a4de8d2786c57bbb5bd77fa3a88bb", // 申请好的Web端开发者Key,首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: [
          "AMap.ToolBar",
          "AMap.Driving",
          "AMap.PolygonEditor",
          "AMap.PlaceSearch",
          "AMap.AutoComplete"
        ] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      })
        .then(AMap => {
          this.map = new AMap.Map("container", {
            //设置地图容器id
            viewMode: "3D", //是否为3D地图模式
            zoom: 10, //初始化地图级别
            center: [113.08, 34.94] //初始化地图中心点位置
          });
          let that = this;
          const autoOptions = {
            input: "mapInput"
          };
          this.auto = new AMap.AutoComplete(autoOptions);
          this.placeSearch = new AMap.PlaceSearch({
            map: this.map,
            // panel: "panel", // 结果列表将在此容器中进行展示。
            // city: "010", // 兴趣点城市
            autoFitView: true, // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
            extensions: "base" //返回基本地址信息
          });
          this.auto.on("select", this.select); //注册监听,当选中某条记录时会触发

          that.map.on("click", e => {
            console.log(that.rowdata.longitude, "地图点击");
            if (
              that.rowdata.longitude == "" ||
              that.rowdata.longitude == null
            ) {
              // 获取经纬度
              that.rowdata.longitude = e.lnglat.lng;
              that.rowdata.latitude = e.lnglat.lat;
              const marker1 = new AMap.Marker({
                icon: "",
                draggable: true,
                position: [e.lnglat.lng, e.lnglat.lat]
              });
              marker1.on("dragging", function (e) {
                console.log(that.rowdata.longitude);
                that.rowdata.longitude = e.lnglat.lng;
                that.rowdata.latitude = e.lnglat.lat;
              });
              that.map.add(marker1);
            }

            // this.map.setFitView();
          });

          this.map.setFitView();
        })
        .catch(e => {
          console.log(e);
        });
      this.initEditor();
    },
    select(e) {
      this.placeSearch.setCity(e.poi.adcode);
      this.placeSearch.search(e.poi.name); //关键字查询查询
    },

    initEditor() {
      let path1 = [];

      path1 = this.pathList || [];

      const polygon1 = new AMap.Polygon({
        path: path1
      });
      this.map.add([polygon1]);
      polyEditor = new AMap.PolygonEditor(this.map);
      // console.log(polyEditor);
      // console.dir(polyEditor);
      polyEditor.on("add", data => {
        console.log(data);
        this.coordList = data.lnglat;
        const polygon = data.target;
        polygon.on("dblclick", () => {
          polyEditor.setTarget(polygon);
          polyEditor.open();
        });
      });
      if (this.cztype == 2) {
        setTimeout(() => {
          polyEditor.setTarget(polygon1);
          polyEditor.open();
        }, 1000);
      }
      polygon1.on("dblclick", () => {
        polyEditor.setTarget(polygon1);
        polyEditor.open();
      });
      let that = this;

      if (this.rowdata.longitude !== "" && this.rowdata.longitude !== null) {
        const marker1 = new AMap.Marker({
          icon: "",
          draggable: true,
          position: [this.rowdata.longitude, this.rowdata.latitude]
        });
        this.map.add(marker1);
        marker1.on("dragging", function (e) {
          that.rowdata.longitude = e.lnglat.lng;
          that.rowdata.latitude = e.lnglat.lat;
          that.$emit("submit", that.pathList, that.rowdata);
        });
      }

      this.map.setFitView();

      return polyEditor;
    },
    createPolygon() {
      polyEditor.close();
      polyEditor.setTarget();
      polyEditor.open();
    },
    editClose() {
      let that = this;
      polyEditor.on("end", function (event) {
        // event.target 即为编辑后的多边形对象
        //多边形的坐标
        this.coordList = event.target.getPath();
        // console.log("coordList", this.coordList);
        const mapList = [];
        this.coordList.forEach(v => {
          console.log("v", v.lng, "--", v.lat);
          mapList.push([v.lng, v.lat]);
        });
        // that.$emit("mapList", mapList);
        that.$emit("submit", mapList, that.rowdata);
      });
      polyEditor.close();
    }
  }
};
</script>

<style lang="scss" scoped>
#container {
  width: 100%;
  // height: 300px;
}

.container {
  position: relative;
  border: 1px solid rgb(204, 204, 204);

  .input-card {
    position: absolute;
    bottom: 15px;
    right: 15px;
  }
  .serch {
    position: absolute;
    top: 5px;
    left: 5px;
  }
}
</style>
//注意在app.vue中加入 否则搜索无效
.amap-sug-result {
  z-index: 9999 !important;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Vue2中使用高德地图进行坐标标点,你可以按照以下步骤进行操作: 1. 首先,在你的HTML文件中导入高德地图的API。你可以使用以下代码: ```html <script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script> ``` 确保将`您申请的key值`替换为你在高德地图开发者平台申请的API密钥。 2. 在Vue组件中导入`coordinateMap.vue`组件。你可以使用以下代码: ```javascript import CoordinateMap from '@/components/coordinateMap.vue' ``` 确保路径正确,根据你的项目结构进行相应的调整。 3. 注册`CoordinateMap`组件。在Vue组件的`components`选项中添加以下代码: ```javascript components: { CoordinateMap } ``` 4. 在Vue组件中使用`CoordinateMap`组件。在模板中添加以下代码: ```html <CoordinateMap @giveLnglat="getLnglat"></CoordinateMap> ``` `@giveLnglat`是一个自定义事件,用于从`CoordinateMap`组件中获取用户当前点击位置的经纬度。`getLnglat`是你在Vue组件中定义的方法,用于处理获取到的经纬度数据。 5. 在Vue组件中定义`getLnglat`方法。在Vue组件的方法中添加以下代码: ```javascript methods: { getLnglat(lnglat) { console.log(lnglat) } } ``` 这个方法将获取到的经纬度数据作为参数,并在控制台中打印出来。你可以根据需要对经纬度数据进行进一步处理或使用。 通过以上步骤,你就可以在Vue2中使用高德地图进行坐标标点了。记得替换相关的API密钥和路径,根据你的具体需求进行适当的修改。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [基于Vue结合高德地图api做的一个坐标拾取组件](https://blog.csdn.net/qq_37131884/article/details/104071925)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值