Vue3,setup,高德地图api,实现地图搜索查询地址功能

打印 上一主题 下一主题

主题 1869|帖子 1869|积分 5607

效果图:
高德地图搜索api支持含糊搜索.

高德地图key申请:

安装依靠:npm i @amap-jsapi-loader -- s
地图组件页面:
  1. <template>
  2.   <div class="main">
  3.     <div class="form">
  4.       <!--搜索框-->
  5.       <input type="text" class="input" v-model="address" id="tipinput"     @keyup.enter="select">
  6.     </div>
  7.     <!--查询列表,高德地图api绑定id-->
  8.     <div class="list" id="list">
  9.     </div>
  10.     <!--地图,需要设置宽高-->
  11.     <div class="map" id="gd_map_d"></div>
  12.   </div>
  13. </template>
  14. <script setup>
  15. import AMapLoader from '@amap/amap-jsapi-loader';
  16. //data
  17. const { proxy } = getCurrentInstance();
  18. const address = ref("");
  19. let geocoder = null;
  20. let MyMap = null;
  21. let map = null;
  22. //methods
  23. const select = () => {
  24.   //构造地点查询类
  25.   var placeSearch = new MyMap.PlaceSearch({
  26.     pageSize: 3, // 单页显示结果条数
  27.     pageIndex: 1, // 页码
  28.     city: "010", // 兴趣点城市
  29.     citylimit: false,  //是否强制限制在设置的城市内搜索
  30.     map: map, // 展现结果的地图实例
  31.     panel: "list", // 结果列表将在此容器中进行展示。
  32.     autoFitView: false // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
  33.   });
  34.   //关键字查询
  35.   placeSearch.search(proxy.address);
  36. }
  37. const getAddress = () => {
  38.   let cc = map.getCenter();
  39.   return new Promise((resolve, reject) => {
  40.     geocoder.getAddress([cc.lng, cc.lat], (status, result) => {
  41.       if (result.regeocode.formattedAddress) {
  42.         resolve(result.regeocode.formattedAddress)
  43.       } else {
  44.         resolve("");
  45.       }
  46.     })
  47.   })
  48. }
  49. //mapinit
  50. window._AMapSecurityConfig = {
  51.   securityJsCode: '填写高德地图Web端(JS API)申请的密钥',
  52. }
  53. AMapLoader.load({
  54.   "key": "填写高德地图Web端(JS API)申请的key", // 申请好的Web端开发者Key
  55.   "version": "2.0",   // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  56.   "plugins": ['AMap.AutoComplete', 'AMap.PlaceSearch', 'AMap.Geocoder'],           // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  57. }).then((AMap) => {
  58.   MyMap = AMap;//保存AMap
  59.   init();
  60. }).catch(e => {
  61.   console.log(e);
  62. })
  63. const init = () => {
  64.   //绘制MyMap实例地图
  65.   map = new MyMap.Map('gd_map_d', {
  66.     zoom: 14, //初始地图级别
  67.     center: [121.473432, 31.22919],
  68.     resizeEnable: true
  69.   })
  70.   geocoder = new MyMap.Geocoder({
  71.     city: "010", //城市设为北京,默认:“全国”
  72.   });
  73.   var auto = new MyMap.AutoComplete({
  74.     input: "tipinput"
  75.   });
  76. }
  77. defineExpose({
  78.   getAddress  //将事件暴露出去
  79. })
  80. </script>
  81. <style scoped lang="scss">
  82. .main {
  83.   width: 100%;
  84.   height: 350px;
  85.   position: relative;
  86.   .map {
  87.     width: 100%;
  88.     height: 100%;
  89.   }
  90.   .form {
  91.     position: absolute;
  92.     left: 10px;
  93.     top: 10px;
  94.     z-index: 999;
  95.     .input {
  96.       width: 180px;
  97.       line-height: 30px;
  98.       padding-left: 5px;
  99.       box-shadow: 0 2px 6px 0 rgb(114 124 245 / 50%);
  100.       outline: none;
  101.       border-radius: 5px;
  102.       border: none;
  103.     }
  104.   }
  105.   .list {
  106.     position: absolute;
  107.     top: 10px;
  108.     right: 10px;
  109.     height: 300px;
  110.     width: 240px;
  111.     z-index: 999;
  112.     .li {
  113.       line-height: 25px;
  114.     }
  115.   }
  116. }
  117. </style>
复制代码
父组件页面
  1. <template>
  2.     <el-dialog v-model="dialogVisible2" title="地图" width="50%" :close-on-click-modal="false">
  3.   <div class="amap-wrapper">
  4.     <div id="Amap">
  5.       <MyMap ref="mymap"></MyMap>
  6.     </div>
  7.   </div>
  8.   <template #footer>
  9.     <span class="dialog-footer">
  10.       <el-button type="primary" @click="getAddress">保存</el-button>
  11.       <el-button @click="dialogVisible = false">
  12.         取消
  13.       </el-button>
  14.     </span>
  15.   </template>
  16. </el-dialog>
  17. </template>
  18. <script setup>
  19. import { computed } from '@vue/reactivity';
  20. import { getCurrentInstance, ref, reactive } from 'vue';
  21. const { proxy } = getCurrentInstance();
  22. const dialogVisible = ref(true);
  23.     const getAddress = () => {
  24.   proxy.$refs.mymap.getAddress().then(res => {
  25.     console.log(res);
  26.   })
  27. }
  28. </script>
复制代码


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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

兜兜零元

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表