【uniapp 写app--实现移动地图坐标始终显示在中间】

uniapp 写app--实现移动地图坐标始终显示在中间


拖动地图,定位跟随移动,都是用的uni 里的东西,根据经纬度反解析地址还没有解决(有机会再来更新)

使用uniapp里的map 组件

在这里插入图片描述

						<view >
							<map style="width: 1200rpx;height: 300px;" id="map" :latitude="latitude" :longitude="longitude" :markers="markers" enable-poi="true"  @tap="updateLocation"  @regionchange="regionchange"></map>
						</view>	
<script>
	export default {
		data() {
			return {
				address:'',
				id:0, // 使用 marker点击事件 需要填写id
				title: 'map',
				latitude: 31.849235,//地图中心的经度or纬度
				longitude: 117.133214,//地图中心的经度or纬度
				markers: [{//定位点1
					id:1,
					latitude: 31.849235,
					longitude: 117.133214,
					iconPath: '/static/images/new-location.png'
				},
				{//定位点2
					id: 2,
					latitude: 31.849235,
					longitude: 117.133214,
					iconPath: '/static/images/location.png'
				}],	
			}
		},
		methods: {
					//获取中心点位置
			getCenterLatLong(){
				let mapCtx =uni.createMapContext("map",this)
				let that = this
				mapCtx.getCenterLocation({
				  success:res=> {
					// 移动定位点的经纬度位置
						that.markers[0].latitude = res.latitude;
						that.markers[0].longitude = res.longitude;
						that.$forceUpdate(); //真机更改数据后界面不变,强制刷新
				  },
				  fail: (res) => {
				  	console.log("失败",res);
				  }
				})
			},
					regionchange(e){
				console.log("regionchange-----------", e);
				if (e.type === 'end') { //在安卓中是 end 事件
					this.getCenterLatLong()  // 地图移动时获取中心点的经纬度
				}else if(e.type ==='regionchange'){ // 在ios中是 regionchange
					this.getCenterLatLong() 
				}
			},
			//获取经纬度
			getLocation() {
				// 新增获取当前定位
				
				//修改获取数据库定位
				uni.getLocation({
					type: 'gcj02',
					geocode: true, //获取城市具体地址
					success: (res) => {
						console.log("获取城市具体地址--", res.address)
						this.latitude = res.latitude
						this.longitude = res.longitude
						this.markers[0].latitude = res.latitude
						this.markers[0].longitude = res.longitude
						this.markers[1].latitude = res.latitude
						this.markers[1].longitude = res.longitude
						// 改变所属区域
						this.bindPickerChange(res.address.district)
						// this.openMap()
					},
					fail: (res) => {
						console.log("获取地址失败了", res);
					}
				})
			},
		}
		
  • 10
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uniapp是一种跨平台的开发框架,可以用于开发手机应用程序。为了实现全局消息推送提示功能,可以结合vuex和websocket技术。 首先,我们可以使用vuex来管理应用程序的状态。vuex是一种专门用于Vue.js开发的状态管理模式,它可以将组件之间共享的状态集中管理,并且可以实现数据的响应式更新。 在vuex中,我们可以创建一个全局的消息状态,包括未读消息的数量和消息列表。当收到新的消息时,可以通过vuex的方法来更新这些状态。组件可以通过vuex的计算属性或者方法来获取当前的未读消息数量和消息列表。 接下来,我们可以使用websocket来实现实时的消息推送功能。websocket是一种在客户端和服务器之间进行双向通信的协议,它可以实现实时的数据传输。 在uniapp中,我们可以使用uni-app-plus插件来支持websocket的使用。可以在应用程序的入口文件中创建一个websocket连接,并且监听消息的入站事件。当收到新的消息时,可以通过websocket发送一个vuex的mutation来更新消息状态。 在组件中,可以监听vuex中消息状态的变化,并且在有新消息到达时显示一个推送提示。可以使用uni-app的原生弹框组件或者自定义样式来实现这个推送提示。 总结起来,使用uniapp开发手机app,结合vuex和websocket可以实现全局消息推送提示功能。通过vuex管理应用程序的状态,通过websocket实现实时的消息传输,结合组件的监听和显示实现全局消息推送提示的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值