【uniapp】 微信小程序--高德地图选取位置

1、制作地图显示层(打开时置于最上层)

2、在地图显示层上制作搜索框、点击地图事件

高德方法

入门指南-微信小程序插件 | 高德地图API (amap.com)

完整代码

<template>
	<view class="sb-view">
		<view>{{address}}</view>
		<view @click="checkMap">定位</view>
		
		<view class="page-body" v-if="showMap">
			<!-- 地图 -->
			<view class="page-section page-section-gap">
				<map style="width: 100%; height: 100vh;" :latitude="latitude" :longitude="longitude" :markers="covers" @tap="getRes" ></map>
			</view>
			
			<!-- 搜索框 -->
			<view class="s-top">
				<input type="text" class="search" placeholder="请输入要查找的地址" :value="searchKey" @input="search" />
				<!-- 搜索建议 -->
				<view class="search-result" v-show="showResult1">
					<view class='column_item' v-for='(item, i) in data1' :key='i' @click="tapOption1(item)" >{{item.name}}</view>
				</view>
				<!-- 点击地图事件 附近地点建议 -->
				<view class="search-result" v-show="showResult">
					<view class='column_item' v-for='(item, index) in data' :key='index' @click="tapOption(item)" >{{item.name}}</view>
				</view>
			</view>
			
			<view class="txt">
				<!-- 显示选择的地点信息 -->
				<view class="add">{{ address1 }}</view>
				<view class="map-btn">
					<view @tap="confirmAddress">确定选择</view>
					<view style="color: black;background-color: #ccc;" @tap="showMap = false">退出地图</view>
				</view>
			</view>
			
			<view class="re-addbtn" @tap="resetBtn">重置</view>
		</view>

		
	</view>
</template>

<script>
	import amapFile from '@/libs/amap-wx.js'
	export default {
		data() {
			return {
				// 地图
				address: '',
				address1: '',
				allAddress: '',
				showMap: false,
				latitude: '22.803751',
				longitude: '113.293719',
				lslatitude: '22.803751',
				lslongitude: '113.293719',
				covers: [{
					id: 1,
					latitude: '22.803751',
					longitude: '113.293719',
					iconPath: '../../static/location.png',
					width: 25,
					height: 30
				}],
				showResult: false,
				showResult1: false,
				searchKey: '',
				data: '',
				data1: [],
				latitude1: '',
				longitude1: '',
				lslatitude1: '',
				lslongitude1: '',
				lat: '',
				lng: '',
				myAddress: '',
			}
		},
		methods: {
			// 打开地图
			checkMap() {
				this.latitude = this.lslatitude
				this.longitude = this.lslongitude
				this.covers = [{
					id: 1,
					latitude: this.latitude,
					longitude: this.longitude,
					iconPath: '../../static/location.png',
					width: 25,
					height: 30
				}]
				this.address1 = this.address
				this.showMap = true
			},
			
			// 重置位置
			resetBtn(){
				this.address = uni.getStorageSync('address')
				this.latitude = uni.getStorageSync('lat')
				this.longitude = uni.getStorageSync('lng')
				this.covers = [{
					id: 1,
					latitude: this.latitude,
					longitude: this.longitude,
					iconPath: '../../static/location.png',
					width: 25,
					height: 30
				}]
				this.searchKey = ''
			},
			
			// 点击获取定位
			getRes(e) {
				var that = this;
				this.showResult = false
				let txLng = e.detail.longitude
				let txLat = e.detail.latitude
				
				var myAmapFun = new amapFile.AMapWX({key:'申请的高德key'});
				myAmapFun.getPoiAround({location: e.detail.longitude + ',' + e.detail.latitude,
					success: function(data){
						// console.log(data);
						that.data1 = data.markers
						that.address1 = data.markers[0].address + ' ' + data.markers[0].name
						that.latitude1 = e.detail.latitude
						that.longitude1 = e.detail.longitude
						that.latitude = e.detail.latitude
						that.longitude = e.detail.longitude
						
						// icon
						that.covers = [{
							id: 1,
							latitude: e.detail.latitude,
							longitude: e.detail.longitude,
							iconPath: '../../static/location.png',
							width: 25,
							height: 30
						}]
						that.showResult1 = true
					},
					fail: function(info){
						//失败回调
						console.log('接口调用失败回调->', info)
					}
				})
			},
			
			// 确定位置
			confirmAddress() {
				if(this.latitude1 != '' && this.longitude1 != ''){
					this.lat = this.latitude1
					this.lng = this.longitude1
					this.allAddress = this.latitude1 + ',' + this.longitude1 + ',' + this.address1
					this.address = this.address1
					this.lslatitude = this.latitude1
					this.lslongitude = this.longitude1
					
					this.searchKey = ''
					this.showMap = false
				}else{
					uni.showToast({
						title: '还未选择地址',
						icon: 'none',
						duration: 2000
					})
				}
			},
			
			// 搜索位置
			search(e) {
				var that = this;
				this.searchKey = e.detail.value
				var myAmapFun = new amapFile.AMapWX({key:'申请的高德key'});
				myAmapFun.getInputtips({
					keywords: this.searchKey,
					city: '佛山',
					citylimit: true, // 是否强制在设置的城市内搜索,默认false
					success: function(data){
						if(data && data.tips){
							that.data = data.tips
							that.showResult = true
							that.showResult1 = false
						}
					}
				})
			},
			
			// 搜索建议 选择新地址
			tapOption(keyWord) {
				this.address1 = keyWord.name
				this.latitude1 = keyWord.location.split(',')[1]
				this.longitude1 = keyWord.location.split(',')[0]
				this.latitude = keyWord.location.split(',')[1]
				this.longitude = keyWord.location.split(',')[0]
				
				this.covers = [{
					id: 1,
					latitude: this.latitude,
					longitude: this.longitude,
					iconPath: '../../static/location.png',
					width: 25,
					height: 30
				}]
				
				this.showResult = false
				this.searchKey = this.address1
			},
			// 点击事件 附件地点建议 选择新地址
			tapOption1(item) {
				this.address1 = item.address + ' ' + item.name
				this.latitude1 = item.latitude
				this.longitude1 = item.longitude
				this.latitude = item.latitude
				this.longitude = item.longitude
				
				this.covers = [{
					id: 1,
					latitude: this.latitude,
					longitude: this.longitude,
					iconPath: '../../static/location.png',
					width: 25,
					height: 30
				}]
				
				this.showResult1 = false
			},
		}
	}
</script>

<style lang="less" scoped>
.page-body{
	position: fixed;
	top: 0;
	left: 0;
	width: 100vw;
	height: 100vh;
	z-index: 999;
	
	.s-top{
		position: absolute;
		top: -1rpx;
		left: 0;
		width: 100%;
		height: 90rpx;
		background-color: #ffffff;
		padding: 20rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		
		.search{
			width: 100%;
			height: 60rpx;
			box-sizing: border-box;
			background-color: #eee;
			border-radius: 30rpx;
			padding-left: 30rpx;
			box-sizing: border-box;
		}
		
		.search-result{
			position: absolute;
			top: 89rpx;
			left: 0;
			width: 100%;
			height: 430rpx;
			background-color: #ffffff;
			overflow-y: auto;
			padding: 20rpx 0;
			box-sizing: border-box;
			z-index: 999;
			
			.column_item{
				width: 100%;
				padding-left: 20rpx;
				box-sizing: border-box;
				line-height: 80rpx;
				border-bottom: #e1e1e1 2rpx dashed;
			}
		}
	}
	
	.txt{
		position: absolute;
		bottom: 0;
		left: 0;
		width: 100%;
		min-height: 200rpx;
		background-color: #ffffff;
		padding-bottom: 120rpx;
		padding-top: 20rpx;
		box-sizing: border-box;
		
		.add{
			width: 100%;
			font-size: 40rpx;
			text-align: center;
		}
		.map-btn{
			position: absolute;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 100rpx;
			display: flex;
			justify-content: space-evenly;
			
			view{
				color: #ffffff;
				width: 44%;
				height: 90rpx;
				line-height: 90rpx;
				text-align: center;
				border-radius: 10rpx;
				background-color: #0590e0;
			}
		}
	}
	
	.photo-box{
		width: 100%;
		height: 20%;
		display: flex;
		justify-content: center;
		align-items: center;
		position: relative;
		
		.photo-out{
			width: 80rpx;
			height: 80rpx;
			line-height: 40rpx;
			text-align: center;
			// border: #FFFFFF 1px solid;
			color: #FFFFFF;
			position: absolute;
			bottom: 50%;
			transform: translateY(40rpx);
			right: 100rpx;
		}
		
		.photo-btn1{
			background-color: #ffffff;
			width: 150rpx;
			height: 150rpx;
			border-radius: 50%;
			display: flex;
			justify-content: center;
			align-items: center;
			
			.photo-btn2{
				background-color: #000;
				width: 135rpx;
				height: 135rpx;
				border-radius: 50%;
				display: flex;
				justify-content: center;
				align-items: center;
				
				.photo-btn3{
					background-color: #ffffff;
					width: 125rpx;
					height: 125rpx;
					border-radius: 50%;
				}
			}
		}
	}
	
	.re-addbtn{
		position: absolute;
		right: 20rpx;
		bottom: 250rpx;
		font-size: 28rpx;
		width: 85rpx;
		height: 85rpx;
		line-height: 85rpx;
		text-align: center;
		border-radius: 50%;
		background-color: #FFFFFF;
	}
}
</style>

效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值