高德地图插件使用

amap插件使用


Vue.use(VueAMap);
VueAMap.initAMapApiLoader({
	key: 'yourkey',
	plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar',
		'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor', 'AMap.Geolocation'],
	// 默认高德 sdk 版本为 1.4.4
	v: '1.4.4',
	uiVersion: '1.0.11'
});
<template>
	<div>
		<div class="amap-page-container">
			<el-amap-search-box class="search-box"
				:search-option="searchOption"
				:on-search-result="onSearchResult">
			</el-amap-search-box>
			<el-amap ref="map" vid="amapDemo"
				:amap-manager="amapManager"
				:center="center"
				:zoom="zoom"
				:plugin="plugin"
				:events="events" class="amap-demo">
				<el-amap-marker :position="center"></el-amap-marker>
			</el-amap>
		</div>
	</div>
</template>
<script>
//引入 获取实例
import {AMapManager} from 'vue-amap';

let amapManager = new AMapManager();
let Geocoder;
export default {
	data() {
		let self = this;
		return {
			amapManager,
			zoom: 12,
			input: '',
			markers: [],
			location: '',
			searchOption: {
				city: '杭州',
				citylimit: false
			},
			center: [120.19, 30.26],
			events: {
				init: (o) => {
					o.getCity((result) => {
						// console.log(result);
					});
				},
				moveend: () => {
				},
				zoomchange: () => {
				},
				click: (e) => {
					self.center = [e.lnglat.lng, e.lnglat.lat];
					Geocoder.getAddress(self.center, function (status, result) { //根据坐标获取位置
						if (status === 'complete' && result.info === 'OK') {
							self.input = result.regeocode.formattedAddress;
							document.querySelector('.search-box-wrapper input').value =
								self.input;
						}
					});
				}
			},
			plugin: [
				{
					pName: 'Geocoder', //使用AMap.Geocoder插件
					events: {
						init(o) {
							Geocoder = o; // o 则是AMap.Geocoder的实例 对外部的Geocoder变量进行赋值,在任何地方就都可以使用了
							//self.center 具体坐标 (数组格式) ,function 回调函数
							o.getAddress(self.center, function (status, result) {
								if (status === 'complete' && result.info === 'OK') {
									//result.regeocode.formattedAddress就是具体位置
									self.input = result.regeocode.formattedAddress;
									//对搜索组件的input进行赋值
									document.querySelector('.search-box-wrapper input').value =
										self.input;
								}
							});
						}
					}
				},
				{
					showMarker: true, //定位成功后在定位到的位置显示点标记,默认:true
					pName: 'Geolocation', // AMap-Geolocation 定位插件
					events: {
						init(o) {
							//getCurrentPosition 获取当前位置的方法
							//注意 虽然进页面就会调用这个方法,但是data()中center要有默认值,不然会报错
							o.getCurrentPosition((status, result) => {
								if (result && result.position) {
									let lng = result.position.lng;
									let lat = result.position.lat;
									self.center = [lng, lat];
									self.loaded = true;
									self.zoom = 14;
									self.$nextTick();
								}
							});
						}
					}
				}
			]
		};
	},
	methods: {
		onSearchResult(pois) {
			this.input = document.querySelector('.search-box-wrapper input').value;
			//这边类似模糊查询 会返回一个数组,我就直接取第一个值了。
			this.center = [pois[0].lng, pois[0].lat];
			this.location = pois[0].lng + ',' + pois[0].lat;
			this.$emit('location', this.location);
		}
	},
	created() {
	}
};
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值