首先引入AMap:
1、在index.html引入AMap
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.6&key=你申请的key&plugin=AMap.Geocoder"></script>
<script src="http://webapi.amap.com/ui/1.0/main.js"></script>

2、在build/webpack.base.conf.js中找到module.exports,在末尾添加以下代码:

externals: {
'AMap': 'AMap'
}

3、在需要调用地图的文件中导入AMap,就可以直接调用AMap的API

import AMap from 'AMap'

注意:

本例中只用到了AMap.Geocoder插件,如果要调用其他的plugin,如AMap.Driving,需要在index.html相应加载(多个plugin用逗号隔开):

<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.6&key=你申请的key&plugin=AMap.Driving,AMap.Geocoder"></script>

下面介绍高德地图模糊搜索地址的使用:

<template>
<el-form class="wrapper" ref="orderForm" :model="orderForm" :rules="addRules" label-width="100px">
<el-form-item label="上车地点:" prop="sname">
<el-input id="sname" v-model="orderForm.sname" type="text"
@focus="initAuto('sname')"
@input="snameSearch"
@keyup.delete.native="deletePlace('sname')"
placeholder="请输入上车地点">
<i
class="el-icon-location-outline el-input__icon"
slot="suffix" title="上车地点">
</i>
</el-input>
</el-form-item>
<el-form-item label="下车地点:" prop="dname">
<el-input id="dname" v-model="orderForm.dname" type="text"
@focus="initAuto('dname')"
@input="dnameSearch"
@keyup.delete.native="deletePlace('dname')"
placeholder="请输入下车地点">
<i
class="el-icon-location-outline el-input__icon"
slot="suffix" title="下车地点">
</i>
</el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" size="small" @click="toSubmit">提交</el-button>
</el-form-item>
<el-form-item v-if="infoVisible">
<div>上车地点:{{orderForm.sname}},经度:{{orderForm.slon}},纬度:{{orderForm.slat}}</div>
<div>下车地点:{{orderForm.dname}},经度:{{orderForm.dlon}},纬度:{{orderForm.dlat}}</div>
</el-form-item>
</el-form>
</template>
<script>
import AMap from 'AMap' export default {
data() {
let validatePlace = (rules, value, callback) => {
if(rules.field==='sname'){
if (value === '') {
callback(new Error('请输入上车地点'));
} else {
if(!this.orderForm.slat || this.orderForm.slat===0) {
callback(new Error('请搜索并选择详细上车地点'));
} else {
callback();
}
}
} else if(rules.field==='dname'){
if (value === '') {
callback(new Error('请输入下车地点'));
} else {
if(!this.orderForm.dlat || this.orderForm.dlat===0) {
callback(new Error('请搜索并选择详细下车地点'));
} else {
callback();
}
}
}
};
return {
orderForm: {
sname: '', // 上车地点
slat: 0, // 上车地点纬度
slon: 0, // 上车地点经度
dname: '', // 下车地点
dlat: 0, // 下车地点纬度
dlon: 0 // 下车地点经度
},
addRules: {
sname: [{required: true, validator: validatePlace, trigger: 'change'}],
dname: [{required: true, validator: validatePlace, trigger: 'change'}]
},
snameAuto: null, // 上车地点Autocomplete
dnameAuto: null, // 下车地点Autocomplete
snameAutoListener: null,// 上车地点Autocomplete监听器
dnameAutoListener: null,// 下车地点Autocomplete监听器
infoVisible: false // 选中地址信息显示
}
},
methods: {
// 实例化Autocomplete
toInitAuto(inputId) {
var auto = null;
AMap.plugin('AMap.Autocomplete',function(){//回调函数
//实例化Autocomplete
let autoOptions = {
city: "", //城市,默认全国
input:inputId //使用联想输入的input的id
};
auto= new AMap.Autocomplete(autoOptions);
//TODO: 使用autocomplete对象调用相关功能
});
return auto;
},
// 初始化搜索地址弹层
initAuto(inputId) {
if(inputId==="sname" && this.snameAuto==null) {
this.snameAuto = this.toInitAuto(inputId);
} else if(inputId==="dname" && this.dnameAuto==null){
this.dnameAuto = this.toInitAuto(inputId);
}
},
// 上车地点搜索
snameSearch() {
if(AMap.event && this.snameAuto){
this.snameAutoListener = AMap.event.addListener(this.snameAuto, "select", (e) => { //注册监听,当选中某条记录时会触发
if(e.poi.location && e.poi.location.getLat()){
this.orderForm.slat = e.poi.location.lat;
this.orderForm.slon = e.poi.location.lng;
this.orderForm.sname = e.poi.name;
this.$refs.orderForm.validateField("sname"); // 触发选择时验证字段
} else {
this.geocoder(e.poi.name, "sname");
}
});
}
},
// 下车地点搜索
dnameSearch() {
if(AMap.event && this.dnameAuto){
this.dnameAutoListener = AMap.event.addListener(this.dnameAuto, "select", (e) => { //注册监听,当选中某条记录时会触发
if(e.poi.location && e.poi.location.getLat()) {
this.orderForm.dlat = e.poi.location.lat;
this.orderForm.dlon = e.poi.location.lng;
this.orderForm.dname = e.poi.name;
this.$refs.orderForm.validateField("dname");// 触发选择时验证字段
} else {
this.geocoder(e.poi.name, "dname");
}
});
}
},
geocoder(keyword, inputValue) {
let geocoder = new AMap.Geocoder({
//city: "010", //城市,默认:“全国”
radius: 1000 //范围,默认:500
});
//地理编码,返回地理编码结果
geocoder.getLocation(keyword, (status, result) => {
if (status === 'complete' && result.info === 'OK') {
let geocode = result.geocodes;
if(geocode.length > 0){
if(inputValue === "sname") {
this.orderForm.slat = geocode[0].location.getLat();
this.orderForm.slon = geocode[0].location.getLng();
this.orderForm.sname = keyword;
this.$refs.orderForm.validateField("sname");
} else if(inputValue === "dname") {
this.orderForm.dlat = geocode[0].location.getLat();
this.orderForm.dlon = geocode[0].location.getLng();
this.orderForm.dname = keyword;
this.$refs.orderForm.validateField("dname");
}
}
}
});
},
// 做删除操作时还原经纬度并验证字段
deletePlace(inputId){
if(inputId === "sname"){
this.orderForm.slat = 0;
this.orderForm.slon = 0;
this.$refs.orderForm.validateField("sname");
} else if(inputId === "dname"){
this.orderForm.dlat = 0;
this.orderForm.dlon = 0;
this.$refs.orderForm.validateField("dname");
}
},
toSubmit(){
this.$refs.orderForm.validate((valid) => {
if(valid){
// submit code...
console.info(this.orderForm);
this.infoVisible = true;
}
});
},
},
beforeDestroy: function () {
// 释放内存
if(this.snameAutoListener){
AMap.event.removeListener(this.snameAutoListener);
}
if(this.dnameAutoListener){
AMap.event.removeListener(this.dnameAutoListener);
}
}
}
</script>
<style>
.wrapper {
margin: 50px;
width: 450px;
}
</style>
 效果图如下:
 
 

高德地图模糊搜索地址(elementUI)的更多相关文章

  1. VUE 高德地图选取地址组件开发

    高德地图文档地址 http://lbs.amap.com/api/lightmap/guide/picker/ 结合步骤: 1.通过iframe内嵌引入高德地图组件 key就选你自己申请的key &l ...

  2. 时时获得高德地图坐标 http://lbs.amap.com/console/show/picker

    1.高德地图标注 在做开发时,或者做高德地图标注的时候,要用到高德地图的坐标,时时获得高德地图坐标 http://lbs.amap.com/console/show/picker 老的高德地图标注地址 ...

  3. Android 打开高德地图、百度地图进行导航;打开第三方App去导航;

    抽成工具类了,复制下来就能直接用了,直接看代码吧: 高德地图Url Api: http://lbs.amap.com/api/amap-mobile/guide/android/navigation ...

  4. 高德地图api实现地址和经纬度的转换(python)

    利用高德地图web服务api实现地理/逆地址编码 api使用具体方法请查看官方文档 文档网址:http://lbs.amap.com/api/webservice/guide/api/georegeo ...

  5. 在H5页面内通过地址调起高德地图实现导航

    项目中用到的一个功能是要通过点击地址来实现打开地图app实现地址导航. 如下图: 实现思路就是在H5页面内通过点击marker图标然后进行当前位置与页面上地址的路程规划与导航. 由于项目中用到的是高德 ...

  6. VUE组件 之 高德地图地址选择

    注:本文基于上一篇文章[ Vue-Cli 3.0 中配置高德地图] ,采用直接引入高德 SDK 的方式来使用高德地图api 一.效果图 二.组件要实现的功能 1. 如果有传入坐标点,则定位到坐标点 2 ...

  7. Java 通过地址获取经纬度 - 高德地图

    一.添加依赖 <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-v ...

  8. vue+Element-ui 的 el-cascader 做高德地图的省市区三级联动并且是异步加载,点击省市区跳转到对应的区(地图可以通过后端返回的点进行标点)

    // 完整版高德地图,可以复制代码直接使用 index.html <script type="text/javascript" src="https://webap ...

  9. Vue异步加载高德地图API

    项目中用到了高德地图的API以及UI组件库,因为是直接把引入script写在index.html中,项目打包后运行在服务器,用浏览器访问加载第一次时会非常慢,主要原因是加载高德地图相关的js(近一分钟 ...

随机推荐

  1. 国人开发的api测试工具 ApiPost

    挺好用的 ApiPost https://www.apipost.cn/download.html 需要注册,免费试用.感觉比postman好用

  2. 深入学习c++--多线程编程(二)【当线程间需要共享非const资源】

    1. 遇到的问题 #include <iostream> #include <thread> #include <chrono> #include <futu ...

  3. v关于使用Glide加载图片失败时显示自己特定的图片

    Glide是Android加载图片的一个框架. 常用加载图片到imageView:Glide.with(this).load(url).into(ImageView imageview). 当加载失败 ...

  4. 【kubernetes secret 和 aws ecr helper】kubernetes从docker拉取image,kubernetes docker私服认证(argo docker私服认证),no basic auth credentials错误解决

    aws ecr helper: https://aws.amazon.com/blogs/compute/authenticating-amazon-ecr-repositories-for-dock ...

  5. Mstar方案软件运行基本原理

    1. MApp_Main.c里有个while(1)循环: 2. 通过 while(1)循环MApp_MultiTasks 里面的 MApp_ProcessUserInput 可以 得到 当前的 u8K ...

  6. jdk 7&8 new features

    7 Diamond Operator(菱形操作符) You can omitted the type declaration of the right when working with Generi ...

  7. [转帖]Hive 快速入门(全面)

    Hive 快速入门(全面) 2018-07-30 16:11:56 琅琊山二当家 阅读数 4343更多 分类专栏: hadoop 大数据   转载: https://www.codercto.com/ ...

  8. 【Webservice】2 counts of IllegalAnnotationExceptions Two classes have the same XML type name

    在使用客户端调用服务端的时候发生了2 counts of IllegalAnnotationExceptions Two classes have the same XML type name的错误, ...

  9. Python31之类和对象1(三大特征:多封继——多疯子)

    一.对象: Python即是面向对象的编程也是面向过程的编程语言,其内部可谓是无处不对象,我们所熟知的列表,字符串等工厂函数本质上都是对象.对象其实是对属性和方法的封装. 属性是对象的静态特征 方法是 ...

  10. golang中锁mutex的实现

    golang中的锁是通过CAS原子操作实现的,Mutex结构如下: type Mutex struct {     state int32                     sema  uint ...