Vue2(vue-amap) 最新高德地图获取坐标与地点信息+搜索功能 ...

打印 上一主题 下一主题

主题 522|帖子 522|积分 1566

效果

第一步:首先我们先去高德地图开放平台申请一个Key

第二步:
需要安装地图插件
  1. npm install vue-amap --save
复制代码
第三步:
  1. // 引入vue-amap
  2. import VueAMap from 'vue-amap'
  3. Vue.use(VueAMap)
  4. VueAMap.initAMapApiLoader({
  5.   key: '你的Key',  // key
  6.   plugin: [
  7.   'AMap.Geolocation',  //定位空间,用来获取和展示用户主机所在的经纬度位置
  8.     ' AMap.Autocomplete ',  //输入提示插件
  9.     ' AMap.PlaceSearch ', //POI搜索插件
  10.     ' AMap.Scale ',   //右下角缩略图插件,比例尺
  11.     ' AMap.OverView ', //地图鹰眼插件
  12.     ' AMap.ToolBar ',  //地图工具条
  13.     ' AMap.MapType ',  //类别切换空间,实现默认图层与卫星图,实施交通层之间切换的控制
  14.     ' AMap.PolyEditor ', //编辑 折线多边形
  15.     ' AMap.CircleEditor ',
  16.     "AMap.Geocoder",     //地图编码
  17.    'AMap.AMapManager',
  18.    'AMap.Marker'
  19.   ],
  20.   // 高德 sdk 版本,默认为 1.4.4
  21.   v: '1.4.4',
  22.   uiVersion: '1.0' //ui版本
  23. })
  24. //高德的安全密钥
  25. window._AMapSecurityConfig = {
  26.   securityJsCode: '你的密钥',
  27. }
复制代码
添加标签
  1.               <div style="border: 1px solid #cccccc">
  2.                   <!-- //搜索框 -->
  3.                   <el-amap-search-box
  4.                     id="search"
  5.                     :search-option="searchOption"
  6.                     :on-search-result="onSearchResult"
  7.                   />
  8.                   <!-- 地图 -->
  9.                   <el-amap
  10.                     class="amap-box"
  11.                     :zoom="amap.zoom"
  12.                     :center="amap.center"
  13.                     :plugin="amap.plugin"
  14.                     :events="amap.events"
  15.                   >
  16.                     <!-- 当前位置图标 -->
  17.                     <el-amap-marker
  18.                       v-for="(marker, index) in amap.markers"
  19.                       :key="'marker' + index"
  20.                       :position="marker.position"
  21.                     />
  22.                     <!-- 位置名称显示 -->
  23.                     <el-amap-text
  24.                       v-for="(marker, index) in amap.markers"
  25.                       :key="'text' + index"
  26.                       :text="marker.text"
  27.                       :offset="marker.offset"
  28.                       :position="marker.position"
  29.                     />
  30.                   </el-amap>
  31.                 </div>
复制代码
数据源:
  1. export default {
  2.   name: "vehicleSpotInspection",
  3.   data() {
  4.     const vm = this;
  5.     return {
  6.       address: "",//需要传给后端的字段
  7.       // form对象
  8.       dataForm: getFormData(),
  9.       // 地图搜索对象
  10.       searchOption: {
  11.         city: "全国",
  12.         citylimit: true,
  13.       },
  14.       lng: 0,
  15.       lat: 0,
  16.       // 地图对象
  17.       amap: {
  18.         zoom: 16,
  19.         center: [116.319802, 39.98294],
  20.         events: {
  21.           // 点击获取地址的数据
  22.           click(e) {
  23.             const { lng, lat } = e.lnglat;
  24.             vm.dataForm.lon = lng;
  25.             vm.dataForm.lat = lat;
  26.             // 这里通过高德 SDK 完成。
  27.             var geocoder = new AMap.Geocoder({
  28.               radius: 100,
  29.               extensions: "all",
  30.             });
  31.             geocoder.getAddress([lng, lat], function (status, result) {
  32.               if (status === "complete" && result.info === "OK") {
  33.                 if (result && result.regeocode) {
  34.                   console.log("点击获取地址的数据", result);
  35.                   vm.dataForm.orgAddr = result.regeocode.formattedAddress;
  36.                   vm.dataForm.province =
  37.                     result.regeocode.addressComponent.province;
  38.                   vm.dataForm.city = result.regeocode.addressComponent.city;
  39.                   vm.dataForm.district =
  40.                     result.regeocode.addressComponent.district;
  41.                   vm.dataForm.lat = lat ? lat.toString() : "";
  42.                   vm.dataForm.lon = lng ? lng.toString() : "";
  43.                   vm.amap.markers = [];
  44.                   const obj = {
  45.                     position: [lng, lat],
  46.                     text: result.regeocode.formattedAddress,
  47.                     offset: [0, 30],
  48.                   };
  49.                   vm.amap.markers.push(obj);
  50.                   vm.sure();
  51.                 }
  52.               }
  53.             });
  54.             // this.$nextTick().then(() => {
  55.             //           // 在这里执行你需要在 DOM 更新后执行的代码
  56.             //         });
  57.           },
  58.         },
  59.         plugin: [
  60.           {
  61.             // 定位
  62.             pName: "Geolocation",
  63.             events: {
  64.               init(o) {
  65.                 // o是高德地图定位插件实例
  66.                 o.getCurrentPosition((status, result) => {
  67.                   console.log("定位:", result);
  68.                   if (result && result.position) {
  69.                     // 设置经度
  70.                     vm.lng = result.position.lng;
  71.                     // 设置维度
  72.                     vm.lat = result.position.lat;
  73.                     vm.address = result.formattedAddress; //获取具体位置
  74.                     console.log("定位address", vm.address);
  75.                     // 设置坐标
  76.                     vm.amap.center = [vm.lng, vm.lat];
  77.                     let op = {
  78.                       position: [vm.lng, vm.lat, vm.address],
  79.                       text: vm.address, //提交之后显示的位置
  80.                       offset: [0, 30],
  81.                     };
  82.                     vm.amap.markers.push(op);
  83.                     // 页面渲染好后
  84.                     // this.$nextTick().then(() => {
  85.                     //   // 在这里执行你需要在 DOM 更新后执行的代码
  86.                     // });
  87.                   }
  88.                 });
  89.               },
  90.             },
  91.           },
  92.         ],
  93.         //
  94.         markers: [],
  95.       },
  96.     };
  97.   },
复制代码
方法:
  1. // 点击获取地址的数据
  2.     // 地图点击事件处理函数
  3.     onMapClick(e) {
  4.       const { lng, lat } = e.lnglat;
  5.       // 更新数据属性
  6.       this.dataForm.lon = lng;
  7.       this.dataForm.lat = lat;
  8.       // 使用 this.$amap 创建 Geocoder 实例
  9.       var geocoder = new this.$amap.Geocoder({
  10.         radius: 100,
  11.         extensions: "all",
  12.       });
  13.       geocoder.getAddress([lng, lat], (status, result) => {
  14.         if (status === "complete" && result.info === "OK") {
  15.           if (result && result.regeocode) {
  16.             console.log("点击获取地址的数据", result);
  17.             this.dataForm.orgAddr = result.regeocode.formattedAddress;
  18.             this.dataForm.province = result.regeocode.addressComponent.province;
  19.             this.dataForm.city = result.regeocode.addressComponent.city;
  20.             this.dataForm.district = result.regeocode.addressComponent.district;
  21.             this.dataForm.lat = lat.toString();
  22.             this.dataForm.lon = lng.toString();
  23.             // 更新标记
  24.             this.amap.markers = [
  25.               {
  26.                 position: [lng, lat],
  27.                 text: result.regeocode.formattedAddress,
  28.                 offset: [0, 30],
  29.               },
  30.             ];
  31.             // 调用其他方法,例如 sure()
  32.             this.sure();
  33.           }
  34.         }
  35.       });
  36.     },
  37.     // 地图搜索回调
  38.     onSearchResult(pois) {
  39.       const vm = this;
  40.       vm.amap.markers = [];
  41.            // 根据是否有搜索结果来启用或禁用搜索框
  42.            this.searchDisabled = pois && pois.length === 0;
  43.       let latSum = 0;
  44.       let lngSum = 0;
  45.       console.log("地图回调", pois);
  46.       if (pois.length > 0) {
  47.         pois.forEach((poi, index) => {
  48.           const { lng, lat } = poi;
  49.           if (index === 0) {
  50.             lngSum = lng;
  51.             latSum = lat;
  52.             const obj = {
  53.               position: [poi.lng, poi.lat],
  54.               text: poi.name,
  55.               offset: [0, 30],
  56.             };
  57.             vm.amap.markers.push(obj);
  58.             console.log("地图搜索回调", poi);
  59.             vm.dataForm.orgAddr = poi.name;
  60.             vm.dataForm.lat = poi.lat ? poi.lat.toString() : "";
  61.             vm.dataForm.lon = poi.lng ? poi.lng.toString() : "";
  62.           }
  63.         });
  64.         vm.amap.center = [lngSum, latSum];
  65.       }
  66.     },
  67.     // 定位成功后更新位置信息的函数
  68.     updateLocationInfo(position) {
  69.       this.dataForm.lat = position.lat;
  70.       this.dataForm.lng = position.lng;
  71.       this.dataForm.orgAddr = position.formattedAddress; // position 对象中有 formattedAddress 属性
  72.       console.log("更新位置", this.dataForm);
  73.       // 更新地图中心点和标记
  74.       this.amap.center = [this.dataForm.lng, this.dataForm.lat];
  75.       this.amap.markers = [
  76.         {
  77.           position: [this.dataForm.lng, this.dataForm.lat],
  78.           text: this.address,//这里改成后台提供的字段可以提交给后台
  79.           offset: [0, 30],
  80.         },
  81.       ];
  82.     },
  83.     // 提交方法
  84.     sure() {
  85.       const vm = this;
  86.       console.log(this.amap.markers);
  87.       this.$emit("updateLocation", vm.dataForm);
  88.     },
复制代码
碰到的问题:1.有的可能会提示 找不到AMap模块 可以引入 import AMap from 'AMap'

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81428

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表