下面我来给大家介绍一些高德地图的使用:
大家能看到这边文章,估计是已经了解了vue-element-admin这个前端框架或者你们公司正在使用这个框架来开发后台管理界面的。那我就不做关于vue-element-admin的介绍了。
话不多说,开始正题:
先说说下面实现的功能的(不多说,直接上图):
上面的功能很好的实现定位,可以实现两种定位方式,一个是鼠标点击手动定位,另外一个是输入地址自动电位,并且让他们自动输入填写到form表的相关位置如下:
npm install vue-amap --save
<div class="amap-page-container"> <el-input placeholder="请输入你要定位的地址(然后点击地图就能定位)" v-model="address"> <i slot="prefix" class="el-input__icon el-icon-search"></i> </el-input> <el-button type="primary" @click="searchMap()">清空地址栏</el-button> <span class="tishi"> {{tishi}}</span> <el-amap vid="amap" :plugin="plugin" class="amap-demo" :center="center" :zoom="zoom" :events='events'> <!-- 点击显示标记 --> <el-amap-marker v-for="(marker, index) in markers" :key="marker.index" :position="marker.position" :icon="marker.icon" :title="marker.title" :events="marker.events" :visible="marker.visible" :draggable="marker.draggable" :vid="index"></el-amap-marker> </el-amap> </div>
data () { let self = this; return { list: null, total: 0, listLoading: true, listQuery: { page: 1, per_page: 10, token:getToken() }, dialogStatus: '', dialogFormVisible: false, temp: { name:'', phone:'', stores:'', address:'', longitude:'', latitude:'', password:'', token:getToken() }, textMap: { create: '添加' }, rules: { phone: [{ required: true, message: '联系电话不得为空', trigger: 'blur' }], name: [{ required: true, message: '姓名不得为空', trigger: 'blur' }], stores: [{ required: true, message: '店名不得为空', trigger: 'blur' }], address: [{ required: true, message: '地址不得为空', trigger: 'blur' }], longitude: [{ required: true, message: '经度不得为空', trigger: 'blur' }], latitude: [{ required: true, message: '纬度不得为空', trigger: 'blur' }] }, tishi:'', imageUrl: '', //从这里下去是地图有关的 address: '', //获取的位置 zoom: 4, // 地图缩放 center: [121.59996, 27.197646], // 初始中心 lng: 0, //经纬度 lat: 0, loaded: false, // 点击显示的标记默认的定位 markers: [/* { position: [121.59996, 31.197646] } */], // 自动定位到当前位置 plugin: [{ timeout: 100, //超过10秒后停止定位,默认:无穷大 panToLocation: true, //定位成功后将定位到的位置作为地图中心点,默认:true zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:f pName: 'Geolocation', events: { click(e) { alert(1) }, init(o) { // o 是高德地图定位插件实例 o.getCurrentPosition((status, result) => { console.log(status, result); console.log(result.formattedAddress); if (result && result.position) { self.address = result.formattedAddress; self.lng = result.position.lng; self.lat = result.position.lat; self.center = [self.lng, self.lat]; self.loaded = true; self.$nextTick(); } }); } } }], // 点击地图获取当前位置并显示标记 events: { click(e) { let {lng,lat} = e.lnglat; self.lng = lng; self.lat = lat; // 这里调用高德 SDK 方法。 var geocoder = new AMap.Geocoder({ radius: 1000, extensions: "all" }); if(self.address.length > 3){ geocoder.getLocation(self.address, function (status, res) { if (status === 'complete' && res.info === 'OK') { self.temp.longitude=res.geocodes[0].location.lng, self.temp.latitude=res.geocodes[0].location.lat, self.temp.address=self.address self.markers=[{ position: [res.geocodes[0].location.lng,res.geocodes[0].location.lat], }] }else{ self.tishi="您输入的地址无法定位到,请先清空地址栏后用鼠标点击选中!"; } }); }else{ self.temp.longitude=lng, self.temp.latitude=lat, self.markers=[{ position: [lng,lat], }] } } } //到这来结束 } },
methods: { searchMap() { this.address='' this.tishi='' },
以上就是代码,是不是特别简单啊!你们能看到这篇文章,说明你们对vue很了解了,我做过多的说明也是多余的了