uni-app APP端map组件 marker自定义弹框组

最近用uni-app 在写地图方面的需求 ,主要是自定义 弹框组,由于uni-app 的map组件对APP端适配的不好,在H5上正常运行,但是在APP端就莫名其妙的有问题,所以就用renderjs实现,代码如下

<template>
	<view>
		<view class="location" @click="getLocation">
			<image style="width: 50rpx; height: 50rpx;" src="/static/image/home/storeIcon.png" mode=""></image>
		</view>
		<view id="amap" :markerList="markerList" :change:markerList="amap.updateEcharts" :latlon="latlon"
			:change:latlon="amap.getLatlon" :style="{ width: '100%', height: 800 + 'px' }">
		</view>
		<uni-popup ref="popRef">
			<view class="popup-content">
				<view>{{makerInfo.latitude}}</view>
				<view>{{makerInfo.longitude}}</view>
				<view>{{makerInfo.name}}</view>
			</view>
		</uni-popup>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				markerList: [{
					online: true,
					latitude: 34.257674,
					longitude: 108.951027,
					name: 'play-house'
				}],
				latlon: {
					latitude: '',
					longitude: '',
				},
				makerInfo: {}
			}
		},
		methods: {
			getLocation() {
				uni.showLoading({
					title: '定位中'
				})
				let that = this
				uni.getLocation({
					type: 'gcj02',
					altitude: true,
					success: function(res) {
						that.markerList.unshift({
							online: true,
							latitude: res.latitude,
							longitude: res.longitude,
							name: '目前的位置'
						})
						that.latlon = {
							latitude: res.latitude,
							longitude: res.longitude
						}
						uni.hideLoading()
					},
					fail: function() {
						console.log('获取位置信息失败');
						uni.hideLoading()
					}
				});
			},
			onViewClick(idx) {
				this.makerInfo = this.markerList[idx]
				this.$refs.popRef.open('center')
			}
		}
	}
</script>


<script lang="renderjs" module="amap">
	export default {
		data() {
			return {
				show: false,
				ownerInstanceObj: null, //service层对象
				map: null,
				markerList: [],
				dataIndex: 0,
				latlon: {}
			}
		},
		mounted() {
			this.init()
		},
		methods: {
			init() {
				if (typeof window.AMap == 'function') {
					this.initAmap();
				} else {
					const script = document.createElement('script');
					script.src = 'https://webapi.amap.com/maps?v=1.4.15&key=高德地图key;
					script.onload = this.initAmap.bind(this);
					document.head.appendChild(script);
				}
			},
			initAmap() {
				
				this.map = new AMap.Map('amap', {
					center: [this.markerList[0].longitude, this.markerList[0].latitude],
					zoom: 15,
				})
				//卫星图配置
				// var satelliteLayer = new AMap.TileLayer.Satellite();
				// this.map.add([satelliteLayer]);
				this.map.on('complete', () => {
					this.initMarkers()
				})
			},
			initMarkers() {
				let that = this;
				this.markerList.forEach((item, index) => {
					var blueIcon = new AMap.Icon({
						size: new AMap.Size(35, 34), // 设置图标大小
						image: 'https://fh-platform-test.obs.cn-east-3.myhuaweicloud.com/2024/1/7/1743806505389391873.png', // 设置图标样式
						imageOffset: new AMap.Pixel(0, 0), // 设置图标偏移
						imageSize: new AMap.Size(35, 34) // 设置图标尺寸
					});
					//添加点标记
					let marker = new AMap.Marker({
						position: new AMap.LngLat(Number(item.longitude), Number(item.latitude)),
						icon: blueIcon,
						draggable: true, // 设置 marker 可拖拽
						// zooms: [2, 12], //点标记显示的层级范围,超过范围不显示
						title: item.name
					})
					marker.on('click', (e) => {
						that.dataIndex = index
						that.onClick(that.ownerInstanceObj)
					})

					this.map.add(marker)

				})
			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				this.markerList = newValue
				this.ownerInstanceObj = ownerInstance
				this.init()
			},
			getLatlon(newValue) {
				this.latlon = newValue
				this.initAmap()
			},
			onClick() {
				this.$ownerInstance.callMethod('onViewClick', this.dataIndex)
			}
		}
	}
</script>
<style lang="scss" scoped>
	.popup-content {
		width: 500rpx;
		height: 500rpx;
		background-color: #fff;
	}

	.location {
		width: 35px;
		height: 35px;
		background: #0079FE;
		display: flex;
		justify-content: center;
		border-radius: 50%;
		align-items: center;
		position: absolute;
		bottom: 100px;
		right: 30px;
		z-index: 55;
	}
</style>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值