当前位置: 首页 > news >正文

vue 使用插件高德地图--vue-amap

 第一步:安装 vue-amap

npm install vue-amap

第二步:在你的 Vue 项目中注册 vue-amap

// main.js
import Vue from 'vue';
import VueAMap from 'vue-amap';Vue.use(VueAMap);VueAMap.initAMapApiLoader({// 高德开发者平台申请key值key: 'cc9c098d8c07c8fbaed05cc8cca2c71c',// plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar' ,'AMap.mapStyle', 'AMap.PolyEditor', 'AMap.CircleEditor'],// 默认高德 sdk 版本为 1.4.4// v: '2.0.0',// theme: 'blue',// mapStyle: 'amap://styles/dark', // 替换为你自己的样式ID
});

 第三步:在你的组件中使用地图点标记里面包含了点击事件

<template><div class="mapClass"><el-amap class="amap-box" ref="map" :amap-manager="amapManager" :vid="'amap-vue'" :zoom="zoom" :plugin="plugin" :center="center" :events="events"><!-- 标记 --><el-amap-marker v-for="(marker, index) in markers" :key="index" :position="marker.position" :title="marker.title" :content="marker.content"><div @click.stop="showInfoWindows(marker)" class="marker-icon-container"><img style="width: 100%; height: 100%" src="../../static/1.jpg" alt="marker icon" /><view class="">{{ marker.content }}</view></div></el-amap-marker></el-amap></div>
</template>
<script>
import { AMapManager, lazyAMapApiLoaderInstance } from 'vue-amap';
export default {data() {return {zoom: 17,markers: [{ position: [121.349402, 31.228667], title: 'Marker 1', content: 'This1', icon: 'https://lmg.jj20.com/up/allimg/4k/s/02/2109250006343S5-0-lp.png' },{ position: [121.329402, 31.228667], title: 'Marker 2', content: 'This2', icon: 'https://lmg.jj20.com/up/allimg/4k/s/02/2109250006343S5-0-lp.jpg' }// 添加更多的标记对象...],lng: 0,lat: 0,amapManager: new AMapManager(),loaded: true,center: [121.329402, 31.228667],events: {init: this.handleMapInit,moveend: this.handleMoveEnd,zoomchange: this.handleZoomChange,complete: this.handleMapComplete,click: this.handleClick},plugin: [// {// 	// 定位// 	pName: 'Geolocation',// 	events: {// 		init(o) {// 			// o是高德地图定位插件实例// 			o.getCurrentPosition((status, result) => {// 				if (result && result.position) {// 					// 设置经度// 					this.lng = result.position.lng;// 					// 设置维度// 					this.lat = result.position.lat;// 					// 设置坐标// 					this.center = [this.lng, this.lat];// 					this.markers.push([this.lng, this.lat]);// 					// load// 					this.loaded = true;// 					// 页面渲染好后// 					this.$nextTick();// 				}// 			});// 		}// 	}// }// {// 	// 工具栏// 	pName: 'ToolBar',// 	events: {// 		init(instance) {// 			// console.log(instance);// 		}// 	}// },// {// 	// 鹰眼// 	pName: 'OverView',// 	events: {// 		init(instance) {// 			// console.log(instance);// 		}// 	}// },// {// 	// 地图类型// 	pName: 'MapType',// 	defaultType: 0,// 	events: {// 		init(instance) {// 			// console.log(instance);// 		}// 	}// },// {// 	// 搜索// 	pName: 'PlaceSearch',// 	events: {// 		init(instance) {// 			// console.log(instance)// 		}// 	}// }]};},methods: {handleMapInit(o) {console.log(o.setMapStyle());// console.log(this.$refs.map.$$getInstance())o.getCity((result) => {console.log(result);});// o.setMapStyle('amap://styles/blue');lazyAMapApiLoaderInstance.load().then(() => {// self.initSearch();});},handleMoveEnd() {// 处理 moveend 事件},handleZoomChange() {// 处理 zoomchange 事件},handleMapComplete(o) {console.log(o);// 处理 complete 事件},handleClick(e) {// 处理 click 事件// ... 你的点击事件处理代码 ...console.log('e', e);},showInfoWindows(marker) {console.log('marker');// 在此处处理点击标记后的事件,例如显示信息窗口...}// 你可能还有其他的方法...}
};
</script><style scoped>
.mapClass {width: 400px;height: 400px;/* 根据您的需求设置地图容器的样式 */
}.marker-icon-container {width: 40px;height: 40px;/* 根据您的需求设置标记图标容器的样式 */
}
</style>

http://www.lryc.cn/news/126950.html

相关文章:

  • 减速比如何计算
  • HarmonyOS/OpenHarmony应用开发-ArkTSAPI组件总体分类与说明(下)
  • 势函数和鞅的停时定理
  • 途乐证券-炒股开户流程是怎样的?
  • Eclipse如何设置快捷键
  • 刷享全球美好 中信银行信用卡推出跨境消费系列活动
  • LeetCode算法心得——限制条件下元素之间的最小绝对差(TreeSet)
  • MySQL表的基础操作(crud)
  • vue中的activated和deactivated
  • unity 发布报错 The type or namespace name `UnityEditor‘ could not be found.
  • 在ubuntu中将dict.txt导入到数据库sqlite3
  • nginx 代理postgresql
  • 小程序 CSS-in-JS 和原子化的另一种选择
  • flutter项目 环境搭建
  • PG-DBA培训12:PostgreSQL物理备份与恢复实战
  • 饿了么大数据开发凉经
  • 前端安全:XSS 与 CSRF 安全防御
  • 应用层读取wfp防火墙阻断记录
  • web基础和tomcat的安装,部署jpress应用
  • idea git命令使用
  • 软件测试技术之单元测试—工程师 Style 的测试方法
  • C#学习....
  • C语言暑假刷题冲刺篇——day2
  • springcloud3 hystrix实现服务降级的案例配置2
  • 第 3 章 稀疏数组和队列(1)
  • 7-10 奇偶分家
  • 使用词向量以数学方式查找具有相似含义的单词
  • opencv实现以图搜图
  • 爬虫工作中代理失效了怎么处理?
  • 使用虚拟环境conda安装不同版本的cuda,cudnn,pytorch