vue项目中使用高的地图vue-map,带标注、点击选址和搜索功能

第一步:安装依赖

npm install vue-amap --save

第二步:在main.js中

import AMap from "vue-amap";
Vue.use(AMap);
AMap.initAMapApiLoader({
  key: "开发者申请的key", 
  plugin: ["AMap.Scale", "AMap.OverView", "AMap.ToolBar", "AMap.MapType"],
  uiVersion: "1.0.11", // ui库版本,不配置不加载,
  v: "1.4.4"
});

第三步:在demo.vue中使用

                <el-amap 
                  ref="map" 
                  vid="amapDemo" 
                  :amap-manager="amapManager" 
                  :center="center" 
                  :zoom="zoom" 
                  :plugin="plugin" 
                  :events="events" 
                  class="amap-demo">
                  <el-amap-marker vid="amapDemo" :position="center"></el-amap-marker>
                </el-amap>
import AMap from "vue-amap";
let amapManager = new AMap.AMapManager();
data(){
    return{
      amapManager,
      zoom: 12,
      center: [121, 31],
      events: {
        init: o => {
          console.log(o.getCenter());
          // console.log(this.$refs.map.$$getInstance());
          o.getCity(result => {
            console.log(result);
          });
        },
        moveend: () => {},
        zoomchange: () => {},
        click: e => {
          console.log(e);
          this.center = [e.lnglat.lng, e.lnglat.lat];//点击选择新地址为中心点
          const msg = {
            key: "您申请的key值",
            location: this.center.join()
          };
          this.$$.ajax({
            url: "https://restapi.amap.com/v3/geocode/regeo",
            data: msg,
            success: data => {
              console.log(data);
              let d = data.regeocode;
              if (d) {
                this.address = d.formatted_address;//点击选择新地址并获取地址的名称
              }
            }
          });
        }
      },
      plugin: [
        "ToolBar",
        {
          pName: "MapType",
          defaultType: 0,
          events: {
            init(o) {
              console.log(o);
            }
          }
        }
      ],
    }
}

⚠️注意:

附:amap搜索建议:

    const msg = {
        key: "你申请的key值",
        keywords: this.address
      };    
    this.$http
        .get("https://restapi.amap.com/v3/assistant/inputtips", {
          params: msg,
          withCredentials: false //一定要加这个,不然会报跨域的错(如果你封装的$http没有设置withCredentials:true就不用加这行)
        })
        .then(res => {
          console.log(res.data);
          let data = res.data;
          this.tips = data.tips;
        })
        .catch(error => {
          this.$message.error(error);
        });

附:vue-amap官网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值