vue引入高德地图,poi搜索,点击选点,标记点

1.在public/index.html,用script标签引入的,如果需要poi搜索务必加上密匙和plugin=AMap.Autocomplete,AMap.PlaceSearch,否则不会显示

    <script type="text/javascript">
      window._AMapSecurityConfig = {
          securityJsCode:'你的密匙',
      }
</script>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=你的key&plugin=AMap.Autocomplete,AMap.PlaceSearch"></script> 
    <script type="text/javascript" src="https://webapi.amap.com/demos/js/liteToolbar.js"></script>

2.上代码

<template>
  <div>
    <el-input
      id="keyword"
      v-model="addres"
      placeholder="输入关键字选取地点"
      style="width: 300px"
    />
    <div id="container_jinfeishuju"></div>
  </div>
</template>
<script>
export default {
  props: {
    form: Object,
  },
  data() {
    return {
      addres: "",
      districtList: "",
      cityCode: "",
    };
  },
  mounted() {
    this.initMap();
  },
  methods: {
    //  递归查询城市编码换算区域编码
    recursion(list, code) {
      for (let i = 0; i < list.length; i++) {
        if (list[i].citycode == code) {
          this.cityCode = list[i].adcode;
          return ;
        }
        if (list[i].districtList && list[i].districtList.length > 0) {
          this.recursion(list[i].districtList, code);
        }
      }
    },
    // 初始化加载地图
    initMap() {
      let that = this;
      let map = new AMap.Map("container_jinfeishuju", {
        center: [113.618917, 34.748348],
        zoom: 13,
        resizeEnable: true,
      });
      //实时路况图层
      let trafficLayer = new AMap.TileLayer.Traffic({
        zIndex: 10,
      });
      // 将创建的点标记添加到已有的地图实例:
      map.add(trafficLayer); //添加图层到地图
      map.on("click", function (ev) {
        console.log(ev);
        that.marker(ev.lnglat.lng, ev.lnglat.lat, map, false);
        that.getCode(ev.lnglat.lng, ev.lnglat.lat, false, "");
      });
      // 父组件传值解析地址 并回显
      if (that.form.id !== 0) {
        that.addres = that.form.placeName;
        that.marker(that.form.placeLon, that.form.placeLat, map, true);
      }

      AMap.plugin(
        ["AMap.Autocomplete", "AMap.PlaceSearch", "AMap.DistrictSearch"],
        function () {
          let autoOptions = {
            city: "郑州", //城市,默认全国
            input: "keyword", //使用联想输入的input的id
            extensions: "all",
          };
          let autocomplete = new AMap.Autocomplete(autoOptions);
          let placeSearch = new AMap.PlaceSearch({
            city: "郑州",
            //   map: map,
            extensions: "base",
          });
          AMap.event.addListener(autocomplete, "select", function (e) {
            console.log(e);
            //TODO 针对选中的poi实现自己的功能
            placeSearch.setCity(e.poi.adcode);
            placeSearch.search(e.poi.name);
            that.getCode(
              e.poi.location.lng,
              e.poi.location.lat,
              true,
              e.poi.name
            );
            //   that.$emit("MapObj", obj);
            that.marker(e.poi.location.lng, e.poi.location.lat, map, true);
          });
          // 获取城市编码和区域编码
          let districtSearch = new AMap.DistrictSearch({
            // 关键字对应的行政区级别,country表示国家
            subdistrict: 2,
            level: "country", //查询行政级别为 市
            extensions: "all",// 基础信息base,all是返回信息
          });
          districtSearch.search("中国", function (status, result) {
            // 查询成功时,result即为对应的行政区信息
            that.districtList = result.districtList[0].districtList;
            console.log(status, result, "区域");
          });
        }
      );
    },
    // 解析地址
    getCode(lng, lat, type, name) {
      let that = this;
      let district = null;
      AMap.plugin(["AMap.Geocoder", "AMap.DistrictSearch"], function () {
        // 引入 Geocoder 逆向地理编码插件,把经纬度转为地址
        const geocoder = new AMap.Geocoder({
          radius: 1000, // 以给定坐标为中心点,单位:米。取值范围:0-3000。默认值:1000
          extensions: "all", // 返回信息的详略。默认值:base,返回基本地址信息;取值为:all,返回地址信息及附近poi、道路、道路交叉口等信息
          batch: true,
        });
        const lnglat = [lng, lat];
        console.log(lnglat);
        geocoder.getAddress(lnglat, async (status, result) => {
          await that.recursion(
            that.districtList,
            result.regeocode.addressComponent.citycode
          );
          // 传入经纬度,根据给定坐标进行解析,把经纬度转为地址
          let obj = {
            placeName: type ? name : result.regeocode.pois[0].name,
            cityCode: that.cityCode,
            placeLon: lng,
            placeLat: lat,
          };

          that.$emit("MapObj", obj);
          that.addres = type ? name : result.regeocode.pois[0].name;
        });
      });
    },
    // 根据点击事件或者搜索事件生成坐标点
    marker(lat, lng, map, type) {
      let marker = new AMap.Marker({
        position: new AMap.LngLat(lat, lng), // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
        title: "当前位置",
      });
      if (type) {
        map.setCenter([lat, lng]);
      }
      map.clearMap(); //清除所有遮罩物
      map.add(marker);
    },
  },
};
</script>
<style scoped>
#container_jinfeishuju {
  padding: 0px;
  margin: 0px;
  width: 100%;
  height: 500px;
  margin-top: 15px;
}
#keyword {
  z-index: 1000000000000000;
}
</style>
<style>
.auto-item {
  font-size: 14px;
  letter-spacing: 1px;
}
</style>

在这里插入图片描述
在这里插入图片描述

  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
要让 Vue 中的高德地图标记点击后跳动一下,可以利用高德地图 JavaScript API 的 Marker 类和 Animation 类来实现。 首先,在 Vue引入高德地图 JavaScript API: ```html <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script> ``` 然后,在 Vue 组件中创建地图和标记: ```vue <template> <div ref="mapContainer" style="height: 400px;"></div> </template> <script> export default { data() { return { map: null, marker: null }; }, mounted() { this.initMap(); }, methods: { initMap() { this.map = new window.AMap.Map(this.$refs.mapContainer, { center: [116.397428, 39.90923], zoom: 13 }); this.marker = new window.AMap.Marker({ position: [116.397428, 39.90923], map: this.map, animation: 'AMAP_ANIMATION_DROP' }); this.marker.on('click', () => { this.marker.setAnimation('AMAP_ANIMATION_BOUNCE'); }); } } }; </script> ``` 在上面的代码中,首先在 mounted 钩子函数中调用 initMap 方法创建地图和标记。在 initMap 方法中,我们通过创建 AMap.Map 实例来创建地图,并且通过创建 AMap.Marker 实例来创建标记。在创建标记的时候,我们设置了 animation 属性为 AMAP_ANIMATION_DROP,这样标记就会在创建时从天上掉下来。然后通过监听标记的 click 事件,在回调函数中设置 animation 属性为 AMAP_ANIMATION_BOUNCE,这样标记就会跳动起来。 注意,要让标记跳动起来,需要在创建标记时设置 animation 属性为 AMAP_ANIMATION_BOUNCE。另外,在改变标记的动画效果时,需要通过 setAnimation 方法来设置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值