如何在前端展现高德地图(定位,搜索框,等功能)

        第一步:

        你得要在高德地图开发者社区申请一个秘钥(包括key以及安全秘钥)为下面开发做准备:

                我的应用 | 高德控制台高德开放平台官网控制台提供了高德开发者Key管理,Key可视化分析等功能。https://console.amap.com/dev/key/app        第二步:

        写代码:

        ①在main.js文件中引入amap,秘钥,安全密钥以及所需要的包文件:

        import AMap from 'vue-amap';
        window._AMapSecurityConfig = {
          securityJsCode:'af38748ce1f62fbd663586a4a790528b',
        }
        Vue.use(AMap);
        // 初始化vue-amap
        AMap.initAMapApiLoader({
          // 高德key
          key: 'c808b6f78842725debf439da734393c0',
          // 插件集合 (插件按需引入)
          plugin: [ 'AMap.Autocomplete','AMap.Geolocation', 'AMap.Geocode',         'AMap.ToolBar', 'AMap.PlaceSearch'],

          v: '1.4.4'

        });

             ②  在vue展示界面 写入组件  

<el-form-item label-width="150px" label="设置位置:">
  <div class="amap-page-container">
    <div :style="{width:'80%',height:'300px'}">
      <el-amap-search-box
        class="search-box"
        :search-option="searchOption"
        :on-search-result="onSearchResult"
      ></el-amap-search-box>
      <el-amap :amap-manager="amapManager" vid="amap" :plugin="plugin" ref="amap" :events="events" class="amap-demo" :center="center">
        <el-amap-marker  :position="center"></el-amap-marker>
      </el-amap>
      <a>{{ this.input }}</a>
    </div>
  </div>
  <el-button type="primary" icon="el-icon-search" size="medium"  :events="plugin.events"> 位置 </el-button>
</el-form-item>

        

import{ AMapManager }from"vue-amap";
let amapManager =new AMapManager();
let Geocoder = null;

③,在初始化变量中写入

//初始化定位
searchOption:{
  city:"合肥",
  citylimit:false,
},
center:[120.19,30.26],
// 显示搜索条件
showSearch: true,
name: undefined,
plugin: [
  {
    pName: 'Geocoder',
    events:{
      init(o) {
        Geocoder=o
        // o 是高德地图定位插件实例
        o.getAddress(self.center, function (status, result) {
          if (status === "complete" && result.info === "OK") {
            //result.regeocode.formattedAddress就是具体位置
            self.input = result.regeocode.formattedAddress;
            //对搜索组件的input进行赋值
            self.$refs.searchBox.keyword = self.input;

          }
        });
      },
    },
  },
  {
    showMarker: true, //定位成功后在定位到的位置显示点标记,默认:true
    pName: "Geolocation", // AMap-Geolocation 定位插件
    events: {
      init(o) {
        //getCurrentPosition 获取当前位置的方法
        //注意 虽然进页面就会调用这个方法,但是data()中center要有默认值,不然会报错
        o.getCurrentPosition((status, result) => {
          if (result && result.position) {
            let lng = result.position.lng;
            let lat = result.position.lat;
            self.center = [lng, lat];
            self.loaded = true;
            self.zoom = 14;
            self.$nextTick();
          }
        });
      },
    },
  },
],
events:{
  click:(e)=> {
    self.center = [e.lnglat.lng, e.lnglat.lat];
    self.BusinessForm.center = self.center
    Geocoder.getAddress(self.center, function (status, result) { //根据坐标获取位置
      if (status === "complete" && result.info === "OK") {
        self.$refs.searchBox.keyword = self.input;
      }
    });
  }
},
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员慕慕

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

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

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

打赏作者

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

抵扣说明:

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

余额充值