使用高德地图Javascript API实现搜索的自动完成

概述

刚好有一个项目,需要定位,并且需要可以手动设置地址,为了提高用户体验,需要加入搜索框的自动完成功能。

第一步 引用api脚本

<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=**appid**"></script>

第二步 布局要实现自动完成的文本框

<input id="complete" type="text" placeholder="输入您要定位的地址" />

第三步 代码实现

AMap.plugin('AMap.Autocomplete',function(){//回调函数
    var autoOptions = {
        city: "", //城市,默认全国
        input:"complete"//使用联想输入的input的id
    };
    var autocomplete= new AMap.Autocomplete(autoOptions);

    AMap.event.addListener(autocomplete, "select", function(data){
        //TODO 选择后的处理程序,data的格式见 附录
    }); 
});

附录

{
    "type": "select",
    "poi": {
        "id": "B000A80X4B",
        "name": "北京市建设工程专业劳务发包承包交易中心(西城区政协北)",
        "district": "北京市西城区",
        "adcode": "110102",
        "location": {
            "I": 39.873013,
            "C": 116.351675,
            "lng": 116.351675,
            "lat": 39.873013
        },
        "address": "广安门南街甲68号",
        "typecode": "130100"
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在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中实现高德地图搜索功能。用户在输入框中输入关键字后,组件会自动搜索地图,并在地图上显示搜索结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值