uni-app map路线轨迹回放功能及turf.js实现缓冲区渲染(微信小程序)

这篇具有很好参考价值的文章主要介绍了uni-app map路线轨迹回放功能及turf.js实现缓冲区渲染(微信小程序)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

使用uni-app中 map组件实现路线轨迹回放功能。 

uni-app map路线轨迹回放功能及turf.js实现缓冲区渲染(微信小程序)

1、通过接口获取返回的轨迹点。

2、地图的坐标系与轨迹点的坐标系要保持一致,否则轨迹有偏差。点经纬度转换,wgs84togcj02 =》js工具类合集(utils.js)

3、绘制开始结束点,设置地图经纬度。

4、polyline,绘制路线点,属性:[{
                points: arr,//经纬度数组
                color: '#0000FF',//线的颜色
                width: 10,//线的宽度
                borderWidth: 2, //线的厚度
                arrowLine: true, //带箭头的线
                //colorList:[],//彩虹线
                //borderColor:'',//线的边框颜色
                //arrowIconPath:'',//更换箭头图标,在arrowLine为true时生效
                //dottedLine:'',//是否虚线,默认false
            }]

5、include-points:自动缩放展示全部轨迹。

6、触发实时轨迹方法movePoint()。

属性可参考:uniapp - Map地图组件属性示例

GET START | Turf.js中文网 (fenxianglu.cn)

代码示例:文章来源地址https://www.toymoban.com/news/detail-502566.html

<!-- 路线轨迹回放 -->
<template>
	<view class="wrap">
		<map
			id="map"
			style="height: 100vh; width:100%;"
			:latitude="map.latitude"
			:longitude="map.longitude"
			:include-points="polygons[0].points"
			:polyline="polyline"
			:show-location="true"
			:polygons="polygons"
			:markers="markers"
		>
		</map>
		
	</view>
</template>

<script>
import { wgs84togcj02} from '@/utils/utils';
import { lineString } from '@turf/helpers';
import  buffer  from '@turf/buffer';
export default {
	name: '',
	data() {
		return {		
			nextPointIndex: 1,
			map: {//地图对象
				latitude: '',
				longitude: '',
			},
			polyline: [{
				points: [],
				color: '#0000FF',
				width: 10,
				borderWidth: 2
			}],
			polygons:[],
			markers:[],
		}
	},
	created() {
		
	},
	onLoad() {
       
	},
	onShow() {
        this.getTrack();
	},
	methods: {
		//获取轨迹记录
		getTrack(){
			const that = this;
			let geometry=[
                {"time":"2022-08-14 22:55:37","Ing":112.98802691401623,"lat":28.05910306601405},
				{"time":"2022-08-14 22:55:41", "Ing":112.98801582826378,"lat":28.059165228204222},
				{"time":"2022-08-14 22:55:41", "Ing":112.98801096604619,"lat":28.05913404298643},
				{"time":"2022-08-14 22:55:47", "Ing":112.98798123606018,"lat":28.05914796956938}
			]
			let arr = [];
            let arr2 = [];
			that.map.latitude = wgs84togcj02(geometry[0].lng, geometry[0].lat)[1];
			that.map.longitude = wgs84togcj02(geometry[0].lng, geometry[0].lat)[0];
				that.markers.push({
					id: 0,
					latitude: that.map.latitude,
					longitude: that.map.longitude,
					width:24,
					height:32,
					// iconPath: require('../../static/images/touxiang.png'),
					callout:{
						content: '开始:'+e.createdTime,//文本
						color:"#000000",
						fontSize:12,
						borderRadius:4,
						bgColor:"#ffffff",
						padding:2,
						display:"ALWAYS",
						textAlign:"center",
					}
				})
			
			JSON.parse(JSON.stringify(geometry)).forEach((item,index) => {
				let obj = {};
				obj['latitude'] = wgs84togcj02(item.lng, item.lat)[1];
				obj['longitude'] = wgs84togcj02(item.lng, item.lat)[0];
				arr.push(obj);
				arr2.push([item.lng,item.lat]);
				if(index==geometry.length-1 && e.updatedTime){
					that.markers.push({
						id: 1,
						latitude: obj.latitude,
						longitude: obj.longitude,
						width:24,
						height:32,
						// iconPath: require('../../static/images/touxiang.png'),
						callout:{
							content: '结束:'+e.updatedTime,//文本
							color:"#000000",
							fontSize:12,
							borderRadius:4,
							bgColor:"#FFFFFF",
							padding:2,
							display:"ALWAYS",
							textAlign:"center",
						}
					})
				}
			});
			that.polyline = [{
				points: arr,//经纬度数组
				color: '#0000FF',//线的颜色
				width: 10,//线的宽度
				borderWidth: 2, //线的厚度
				arrowLine: true, //带箭头的线
                //colorList:[],//彩虹线
                //borderColor:'',//线的边框颜色
                //arrowIconPath:'',//更换箭头图标,在arrowLine为true时生效
                //dottedLine:'',//是否虚线,默认false
			}];
            
            //绘制缓冲区
			if(arr2.length <1) return;
			var linestrings = lineString(arr2);
			console.log("linestrings",linestrings)
			// 默认单位是千米,与下面的写法一致
			var buffereds = buffer(linestrings, 100, {units: 'meters'});
			var polygonlist = buffereds.geometry.coordinates[0];
			var mapPolygon=[];
			JSON.parse(JSON.stringify(polygonlist)).map((item)=>{
				// 绘制路线点
				mapPolygon.push({
					latitude: wgs84togcj02(item[0], item[1])[1],
					longitude: wgs84togcj02(item[0], item[1])[0]
				});
			})
			that.polygons = [{
				points: JSON.parse(JSON.stringify(mapPolygon)),
				strokeWidth:2,
				strokeColor:'#0070d9',
				fillColor:'#0070d933',
				zIndex:2
			}];
			
			//动态轨迹 方法一(微信小程序中无效果)
			// this.mapContext.moveAlong({
			// 	markerId: 1,
			// 	path: that.polyline[0].points,
			// 	duration: 5000,
			// 	autoRotate:true,
			// 	success(res) {
			// 		console.log('####:',res);
			// 	}
			// });
				
			//动态轨迹 方法二
			this.movePoint();
		},
		//实时轨迹
		movePoint(){
			let durationTime = Math.ceil(30000 / this.polyline[0].points.length)	//默认播放全程使用30秒,计算相连两点动画时长
			this.mapContext.translateMarker({
				duration: durationTime,
				markerId: 1,
				destination: {
					latitude: this.polyline[0].points[this.nextPointIndex].latitude,
					longitude: this.polyline[0].points[this.nextPointIndex].longitude
				},
				animationEnd: res => {
					//播放结束,继续移动到下一个点,最后一个点时结束移动
					if (this.nextPointIndex < this.polyline[0].points.length - 1) {
						this.nextPointIndex++
							this.movePoint()
					} else {
						this.nextPointIndex = 1
						
					}
				}
			})
		},
	}
}
</script>

到了这里,关于uni-app map路线轨迹回放功能及turf.js实现缓冲区渲染(微信小程序)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 基于uni-app的微信小程序Map事件穿透处理

    业务需要在微信小程序中使用地图组件,上面需要有点位及点位的交互,同时地图上也会有一些悬浮的按钮、弹窗之类的。在微信小程序2.8.x的版本之后,地图这种原生组件是支持同层渲染的,也就是可以通过样式控制层级。在开发者工具中表现正常,但是上了真机后会发现点

    2024年02月11日
    浏览(49)
  • 【uni-app】uni-app实现聊天页面功能(小程序)——布局篇

    在工作中使用uni-app参与开发一个小程序,其中要写一个简单的聊天页面,虽然功能不多(只有一个发送文字的功能),但是其中的细节比较多,也踩过很多坑,特此记录下来。要实现的页面如图所示,该篇主要讲讲如何布局(参考了很多文章之后根据页面需求进行一个整合)

    2024年02月05日
    浏览(66)
  • uni-app实现图片上传功能

    效果 代码  

    2024年02月13日
    浏览(50)
  • 【uni-app】通过uni-app基础组件picker实现选择日期、时间的功能示例(完整代码+图文)

    一、获取日期(基础) 二、获取日期和时间(改进)  

    2024年02月11日
    浏览(37)
  • uni-app小程序分享功能实现

    通过onShareAppMessage(OBJECT) 将小程序到分享微信聊天,onShareTimeline()将小程序分享到朋友圈。 api中的参数配置参考文档:https://uniapp.dcloud.net.cn/api/plugins/share.html#onshareappmessage 分为全局引入、单页面引两种方式 全局引入只需要在小程序main.js中引入一次,可以复用,便于维护; 单

    2024年02月05日
    浏览(81)
  • uni-app开发 小程序直播功能

    1、微信后台申请插件开通 2、微信后台开通直播功能 3、代码中接入直播插件AppID 4、【直播组件】如何使用 5、直播组将状态获取 微信开发直播功能,需要企业账号; 需要申请直播功能和插件 1、微信后台申请插件开通 微信后台 登录微信后台 点击设置中的第三方设置 — 添

    2024年02月05日
    浏览(47)
  • uni-app商城中的搜索功能

     一、效果图 二、代码 首先由首页顶部的搜索跳转到真正的搜索页面,输入内容后回车即可完成搜索(假数据,无请求数据 )  ① 首页布局 ② 搜索页面布局 ③ 商品列表页布局  

    2024年02月12日
    浏览(47)
  • uni-app小程序中实现分享功能

    1、在manifest.json文件中配置分享相关信息,包括分享标题、分享图片等。 代码如下: 2、在需要触发分享的页面中,使用uni.navigateToMiniProgram()方法打开分享页面。  3、在被分享的小程序中,可以通过wx.getLaunchOptionsSync()获取到分享时携带的额外数据。  

    2024年02月08日
    浏览(37)
  • uni-app开发小程序中遇到的map地图的点聚合以及polygon划分区域问题

    写一篇文章来记录以下我在开发小程序地图过程中遇到的两个小坑吧,一个是点聚合,用的是joinCluster这个指令,另一个是polygon在地图上划分多边形的问题: 1.首先说一下点聚合问题,由于之前没有做过小程序地图问题,所以浏览了很多资料,最终发现看的多了反而杂乱,而

    2024年02月11日
    浏览(39)
  • 【uni-app微信小程序】实现支付功能

    实现微信支付功能需要在小程序后台配置支付相关信息,并且在前端代码中调用微信支付API进行支付操作。好的, uni-app微信小程序实现支付功能整体流程 大致如下: 注册微信公众平台,并完成开发者资质认证; 在微信商户平台注册商户账号,并完成商户资质认证; 在商户

    2024年02月13日
    浏览(77)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包