vue使用高德地图点聚合,点击显示弹窗

高德地图点聚合,点击地图上的点展示弹窗,再根据不同类型展示不同的图片

html

<div class="public-map" id="container"></div>

js

官网需要的数据格式
在这里插入图片描述

export default {
data() {
	return{
		maplist: [],
		map:null,
	    cluster: null,
	}
},
methods:{
    //请求数据
    getMaplist(){
    	//这里写你自己的请求
    	getlist().then(res=>{
			let arr = []
			let data = res.data
			data.forEach(item=>{
				arr.push({
				//将数据处理成高德地图官网需要的形式
					lnglat:[item.Longitude, item.Latitude]
				}}
			})
			this.maplist=arr
		})
    },
	//初始化地图
	getmap() {
				AMapLoader.load({
						key: '你得key',
						version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
						plugins: ["AMap.Geolocation", "AMap.AutoComplete", 'AMap.PlaceSearch',
							"AMap.Geocoder"
						], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
						AMapUI: {
							// 是否加载 AMapUI,缺省不加载
							version: "1.1", // AMapUI 版本
							plugins: ["overlay/SimpleMarker"], // 需要加载的 AMapUI ui插件
						},
						Loca: {
							// 是否加载 Loca, 缺省不加载
							version: "2.0", // Loca 版本
						},
					})
					.then((AMap) => {
						// console.log(AMap)
						this.map = new AMap.Map("container", { //设置地图容器id
							viewMode: "3D", //是否为3D地图模式
							zoom: 10, //初始化地图级别
							center: [104.937478,35.439575], //初始化地图中心点位置
							//mapStyle: 'amap://styles/darkblue',  //地图颜色
						});
						//此处将数据换成官网需要的格式
						var points = [];
						this.maplist.forEach(item=>{
							points.push({lnglat:item.lnglat})
						})
						var count = points.length;
						//聚合样式
						var _renderClusterMarker = function(context) {
							console.log(context)
							var factor = Math.pow(context.count / count, 1 / 18);
							var div = document.createElement('div');
							var Hue = 180 - factor * 180;
							var bgColor = 'hsla(' + Hue + ',100%,40%,0.7)';
							var fontColor = 'hsla(' + Hue + ',100%,90%,1)';
							var borderColor = 'hsla(' + Hue + ',100%,40%,1)';
							var shadowColor = 'hsla(' + Hue + ',100%,90%,1)';
							div.style.backgroundColor = bgColor;
							var size = Math.round(30 + Math.pow(context.count / count, 1 / 5) * 20);
							div.style.width = div.style.height = size + 'px';
							div.style.border = 'solid 1px ' + borderColor;
							div.style.borderRadius = size / 2 + 'px';
							div.style.boxShadow = '0 0 5px ' + shadowColor;
							div.innerHTML = context.count;
							div.style.lineHeight = size + 'px';
							div.style.color = fontColor;
							div.style.fontSize = '14px';
							div.style.textAlign = 'center';
							context.marker.setOffset(new AMap.Pixel(-size / 2, -size / 2));
							context.marker.setContent(div)
						};
						var _renderMarker = function(context){
							//可以根据数据的不同类型进行判断
							let img = '';
							let content = '';	
							context.data.forEach(item=>{
								if(item.Name=='a'){
								   img=require('你的图片路径')
								}else{
								   img=require('你的图片路径')
								}
							})							
							content = '<img src=' + img + ' style="height: 20px; width: 20px;">';
							var offset = new AMap.Pixel(-9, -9);
							context.marker.setContent(content)
							context.marker.setOffset(offset)
						}
						var that = this
						this.map.plugin(['AMap.MarkerClusterer'], function() {
						      that.cluster = new AMap.MarkerClusterer(
						        that.map, // 地图实例
						        that.maplist, // 海量点组成的数组
						        {
						          gridSize: 30,
						          zIndex: 2,
						          zoomOnClick: false,
						          renderClusterMarker: _renderClusterMarker,
								  renderMarker:_renderMarker
						        })
						    })
							 this.cluster.on('click', (cluster, lnglat, target, markers) => {
								   this.setInfoWindow(cluster.clusterData[0])
							})
					}).catch((e) => {});
			},
			// 窗体信息
			setInfoWindow(item) {
				var content = '这是信息窗体!'
				const infoWindow = new AMap.InfoWindow({
					content, // 使用默认信息窗体框样式,显示信息内容
					offset: new AMap.Pixel(0, -40),
					closeWhenClickMap: true,
				})
				infoWindow.open(this.map,item.lnglat);
			},

		}
}

附加一下高德地图的引用

在public文件夹下的index.html里写入自己的钥

<script type="text/javascript">
      window._AMapSecurityConfig = {
		  securityJsCode: '你早高德地图申请的钥',//密钥
      }
</script>
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
要在Vue3中使用高德地图聚合图,可以按照以下步骤进行: 1. 安装高德地图的JavaScript API ```html <script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script> ``` 2. 在Vue3项目中安装`vue-amap`插件 ```bash npm install vue-amap --save ``` 3. 在`main.js`中引入`vue-amap`插件,并进行配置 ```javascript import VueAMap from 'vue-amap'; Vue.use(VueAMap); VueAMap.initAMapApiLoader({ key: '您申请的key值', plugin: ['AMap.MarkerClusterer'], v: '1.4.15', }); ``` 4. 在Vue3组件中使用高德地图聚合图 ```vue <template> <div> <el-amap :zoom="zoom" :center="center"> <el-amap-marker-clusterer :grid-size="80" :render-clusters="renderClusters"> <el-amap-marker v-for="(item, index) in markers" :key="index" :position="item.position" :ext-data="item"></el-amap-marker> </el-amap-marker-clusterer> </el-amap> </div> </template> <script> export default { data() { return { zoom: 11, center: [116.397428, 39.90923], markers: [ { position: [116.403981, 39.915599] }, { position: [116.398764, 39.914888] }, { position: [116.390786, 39.912356] }, { position: [116.416357, 39.928353] }, { position: [116.368904, 39.913423] }, ], }; }, methods: { renderClusters(data) { return data.map((item) => { return { ...item, icon: new AMap.Icon({ size: new AMap.Size(32, 32), image: 'https://a.amap.com/jsapi_demos/static/images/marker.png', imageSize: new AMap.Size(32, 32), }), }; }); }, }, }; </script> ``` 这样就可以在Vue3项目中使用高德地图聚合图了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值