vue2/3 - 实现高德地图按关键字搜索功能,在vue项目中使用高德地图实现搜索框关键字联想,用户在输入框输入关键词后下方出现推荐联想地址并点击前往功能(详细可运行示例源码,一键复制开箱即用)

效果图

在vue2 / vue3项目网站网页开发中,详细实现高德地图的使用及搜索框,输入关键词后自动出现下拉相关地址并点击搜索功能效果。

提供下方效果图示例代码,一键复制改下样式即可使用。

在这里插入图片描述

完整代码

记得把高德地图key密钥替换成你的!

上方效果图的示例代码

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在Vue2实现高德地图搜索功能,可以按照以下步骤操作: 1. 安装高德地图JavaScript API 在Vue2项目,可以通过npm安装高德地图JavaScript API: ```bash npm install @amap/amap-jsapi-loader --save ``` 2. 创建高德地图组件 在Vue2项目,可以创建一个高德地图组件,用于显示地图和搜索结果: ```vue <template> <div id="map-container"></div> </template> <script> import { AMapLoader } from '@amap/amap-jsapi-loader'; export default { name: 'MapContainer', props: { apiKey: String, // 高德地图API的key searchKeywords: String, // 搜索关键字 }, data() { return { map: null, // 地图对象 markers: [], // 地图标记 }; }, mounted() { this.initMap(); }, methods: { async initMap() { const loader = new AMapLoader({ key: this.apiKey, version: '2.0', }); const { AMap } = await loader.load(); this.map = new AMap.Map('map-container'); if (this.searchKeywords) { this.search(this.searchKeywords); } }, async search(keywords) { const { AMap } = await new AMapLoader({ key: this.apiKey, version: '2.0', }).load(); const placeSearch = new AMap.PlaceSearch({ map: this.map, }); placeSearch.search(keywords, (status, result) => { if (status === 'complete' && result.info === 'OK') { this.markers = result.poiList.pois.map((poi) => { const marker = new AMap.Marker({ position: poi.location, title: poi.name, }); marker.setMap(this.map); return marker; }); } }); }, }, watch: { searchKeywords(newVal) { this.search(newVal); }, }, }; </script> <style> #map-container { width: 100%; height: 100%; } </style> ``` 3. 在父组件使用高德地图组件 在父组件,可以使用上述高德地图组件,并传入高德地图API的key和搜索关键字: ```vue <template> <div> <input v-model="searchKeywords" placeholder="请输入关键字搜索地图" /> <MapContainer apiKey="your_amap_api_key" :searchKeywords="searchKeywords" /> </div> </template> <script> import MapContainer from './MapContainer.vue'; export default { name: 'App', components: { MapContainer, }, data() { return { searchKeywords: '', }; }, }; </script> ``` 这样就可以在Vue2实现高德地图搜索功能用户输入框输入关键字后,组件会自动搜索地图,并在地图上显示搜索结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

街尾杂货店&

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

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

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

打赏作者

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

抵扣说明:

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

余额充值