小程序实现地图找房功能

思路解析:使用小程序的 map 地图组件 ,markers 标记点数据集合 用来生产地图上的标记,用callout 或者label 实现气泡,本文使用的是callout ,最后如何实现点击气泡更新下方展示数据,使用到的是 markers的id属性(注意一定是数字类型不可是字符串)

最终实现效果图
在这里插入图片描述

语法使用的是uniapp,原生小程序和uniapp实现方式大致一致
html

<template>
	<view class="owarp">
		<view class="head">...</view>
		<!-- 地图展示区域 -->
		<view class="center">
			<map id="map" 
			:scale="scale"
			:latitude="lat" 
			:longitude="lng" 
			:markers="markers"
			show-location=true
			@regionchange="changeRegion"
			@callouttap="handleCallout"
			>
			</map>	
		</view>
		<view @click="returnCur" class="gps flex column acenter jcenter">
			<image class="icon44" src="/static/images/home/homesearch.png" mode=""></image>
			<view class="fs20">定位</view>
		</view>
		<view class="foot">
			<view class="flex">
				<image class="imgradus" :src="roomData.main_picture" mode="aspectFill"></image>
				<view class="ml20">
					<view class="fs32 b">{{roomData.hotel_name}}</view>
					<view class="fs24 c8c mt6 w500 ellipsis">距您约 {{roomData.distance}} km</view>
					<view class="flex acenter mt6">
						<image class="icon22" src="/static/images/location.png" mode=""></image>
						<view class="fs24 w500 ellipsis">{{roomData.hotel_address}}</view>
					</view>
					<view class="mt6">
						<text class="b fs24 red">¥</text>
						<text class="b fs40 red">{{roomData.lowest_price}}</text>
						<text class="fs24 c8c">起</text>
					</view>
				</view>
			</view>
			<scroll-view class="itemHeight mt20" scroll-x="true">
				<view class="itemdata" v-for="(item,i) in roomData.room_type" :key="i">
					<view class="flex h174 acenter column between">
						<view class="fs28 themeColor tittop ellipsis">{{item.room_type_name}}</view>
						<view class="fs24 themeColor mt7">{{item.desc}}</view>
						<view class="mt7">
							<text class="b fs22 red">¥</text>
							<text class="b fs34 red">{{item.current_price}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

js

<script>
export default {
	data(){
		return{
			lat:'',
			lng:'',
			scale:13,
			roomData:'',
			markers:[],
			localtion:'',
			mapCtx:''
		}
	},
	onShow() {
	//获取用户定位经纬度
		this.localtion = uni.getStorageSync('localtion')?JSON.parse(uni.getStorageSync('localtion')):''
		this.lat = this.localtion.lat
		this.lng = this.localtion.lng
	},
	mounted(){
		this.getData()
	},
	methods:{
		getData(){ //获取数据生成markers标记
			this.$http('xxx', 'POST', {  //换成自己的接口
				lng: this.localtion.lng,
				lat:this.localtion.lat,
				city_id:this.address.code
			}).then((res) => {
				var maplist = res.data.list
				this.setMarkers(maplist)
			})
		},
		getRoomType(markId){  //获取对应的数据渲染最下方内容
			this.$http('xxx', 'POST', {
				lng: this.localtion.lng,
				lat:this.localtion.lng,
				hotel_id:markId
			}).then((res) => {
				this.roomData = res.data
			})
		},
		changeRegion(){ //中心点标记始终在地图中心位置不动
			let that = this;
			this.mapCtx = uni.createMapContext('map');
			this.mapCtx.getCenterLocation({
				success(res) {
					const latitude = res.latitude
					const longitude = res.longitude
					that.lat=latitude
					that.lng=longitude
				}
			})
		},
		handleCallout(e){  //点击气泡获取验薪数据
			var markId = e.detail.markerId
			this.getRoomType(markId)
		},
		returnCur(){ //点击定位图片 设置中心点回到最原始的位置
			this.lat = this.localtion.lat
			this.lng = this.localtion.lng
		},
		setMarkers(maplist){  //设置标记点
		//具体属性含义和用法可参照官方文档
			maplist.forEach((item)=>{
				this.markers.push({
					latitude: item.lat,
					longitude: item.lng,
					id:item.hotel_id,    //id 要为数字类型,不能为字符串(字符串会生成其他的值和绑定值不一致)
					callout:{
						content:item.hotel_name+' '+'¥'+item.current_price+'起',
						bgColor:'#078591',
						color:'#fff',
						padding:'5',
						fontSize:'13',
						display:'ALWAYS',
						textAlign:'center',
						borderRadius:'14',
						padding:'7'
					}
				}) 
			})
			
		}
	}
}
</script>

css

<style lang="scss" scoped>
.head{
	height: 86rpx;
	background: #fff;
	padding: 0 30rpx;
	.tobtm{
		width: 10rpx;
		height: 10rpx;
	}
}
.mt7{
	margin-top: 7rpx;
}
.w500{
	width: 500rpx;
}
.fb{
	color: #FB5E5E;
}
.imgradus{
	width: 160rpx;
	height: 160rpx;
	border-radius: 9rpx;
}
.center{
	width: 750rpx;
	height:calc(100vh - 545rpx);
}
#map{
	width: 750rpx;
	height: 100%;
	position: relative;
	.img-map{
		position: absolute;
		width: 26rpx;
		height: 40rpx;
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%,-50%);
	}
}
.gps{
	position: fixed;
	bottom: 550rpx;
	right: 30rpx;
	width: 84rpx;
	height: 84rpx;
	background: #FFFFFF;
	border-radius: 10rpx;
	border: 1px solid #ABABAB;
}
.foot{
	height: 459rpx;
	background: #fff;
	position: fixed;
	bottom: 0;
	width: 100%;
	border-radius: 24rpx 24rpx 0 0;
	padding: 40rpx 30rpx;
	box-sizing: border-box;
	.itemHeight{
		height: 175rpx;
		width: 100%;
		white-space: nowrap;
		.itemdata{
			width: 260rpx;
			height: 174rpx;
			background: #EBF5F6;
			border-radius: 10rpx;
			display: inline-block;
			margin-right: 20rpx;
			box-sizing: border-box;
		}
		.h174{
			padding: 20rpx;
			height: 174rpx;
			box-sizing: border-box;
		}
		.tittop{
			width: 220rpx;
			text-align: center;
		}
	}
}
</style>

到此结束

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
好的,以下是一个租房小程序的软件结构说明: 1. 用户界面层:该层是用户与应用程序交互的界面,负责接收用户的输入,并将输出呈现给用户。该层主要由以下模块组成: - 登录/注册模块:提供用户注册和登录功能。 - 首页模块:展示房源信息,提供搜索和筛选功能。 - 房源详情模块:展示房源详细信息,提供预约看房和下单功能。 - 订单管理模块:展示用户的订单信息,提供订单管理功能。 - 个人中心模块:展示用户的个人信息和设置,提供修改和注销功能。 2. 应用逻辑层:该层负责处理用户的请求,并将结果返回到用户界面层。该层主要由以下模块组成: - 用户管理模块:负责处理用户的注册、登录和注销请求。 - 房源管理模块:负责管理房源的信息,包括添加、修改、删除和查询房源信息。 - 订单管理模块:负责处理用户的订单请求,包括下单、取消订单和查询订单信息。 - 支付管理模块:负责处理用户的支付请求,包括支付宝、微信等支付方式。 - 推荐模块:根据用户的历史记录、偏好等信息,为用户推荐符合其需求的房源。 3. 数据访问层:该层负责与数据库进行交互,存储和检索数据。该层主要由以下模块组成: - MySQL模块:负责连接MySQL数据库,实现数据的增删改查等操作。 - Redis模块:负责缓存数据,提高应用程序的访问速度。 - 数据库管理模块:负责管理数据库的结构和数据,包括备份、恢复和优化等操作。 4. 第三方服务层:该层负责与第三方服务进行交互,包括短信服务、邮件服务、地图服务等。该层主要由以下模块组成: - 短信服务模块:负责发送短信验证码和通知等。 - 邮件服务模块:负责发送邮件通知和回执等。 - 地图服务模块:负责提供地图信息和导航功能等。 总体来说,租房小程序的软件结构大致如上所述,不同的实现会有所不同,但核心模块和功能基本相同。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值