uniapp 地图行车轨迹

文章目录

uniapp 地图行车轨迹

官网地图组件:https://uniapp.dcloud.net.cn/component/map.html

官网地图组件控制:https://uniapp.dcloud.net.cn/api/location/map.html#createmapcontext

1、画地图

html 复制代码
<map class="positioning-map"
     id="largeScreenMap"
     :latitude="中心纬度"
     :longitude="中心经度"
     :scale="5"
     :include-points="polyline[0].points"
     :markers="标记点"
     :polyline="路线"
     @markertap="点击标记点时触发"
></map>
js 复制代码
// 地图实例
onReady() {
	this._mapContext = uni.createMapContext("largeScreenMap", this);
}

2、切换地图中心点

js 复制代码
// 获取当前位置
 uni.getLocation({
    type: 'wgs84',
    success: function (res) {
        let { longitude, latitude } = res
        // 将地图中心移动到当前定位点,需要配合map组件的show-location使用
        _this._mapContext.moveToLocation({ longitude: +longitude, latitude: +latitude }) 
        _this.mapCenter = { lat: latitude, lng: longitude }
    }
});

3、画路线

js 复制代码
// 路线
let polyline = [{
    width: 8,
    points: [], // 经纬度数组 [{latitude: 0, longitude: 0}]
    arrowLine: true, //带箭头的线
    color: '#3591FC', // 线的颜色
}]

4、轨迹移动

js 复制代码
// nextPoint 下一个点,moving 是否继续行驶-用于暂停行驶
movePoint(){
	if(!this.polyline[0] return;
	let durationTime = Math.ceil(30000 / polyline[0].points.length)	//默认播放全程使用30秒,计算相连两点动画时长
	this._mapContext.translateMarker({ // 平移marker,带动画
    duration: durationTime,
    markerId: '当前标记点的id', 
    destination: { // 指定 marker 移动到的目标点
        latitude: this.polyline[0].points[this.nextPointIndex].latitude,
        longitude: this.polyline[0].points[this.nextPointIndex].longitude
    },
    animationEnd: res => { // 动画结束回调函数
        //播放结束,继续移动到下一个点,最后一个点时结束移动
        if (this.nextPoint < polyline[0].points.length - 1) {
            this.nextPoint++
            if (this.moving) {
                this.movePoint()
            }
        } else {
            this.nextPoint = 1
        }
    }
})
}

5、标记点及自定义内容

js 复制代码
let marker=[
	{
		id: number-必填,
		latitude: '纬度',
		longitude: '经度',
		width: 18, height: 25,
		callout:{ content: '结束',// 开始
                  bgColor: '#ffffff',
                  padding: 4,
                  borderRadius: 3,
                  display: 'ALWAYS'},// '自定义标记点上方的气泡窗口'- 纯文本内容 - content 显示标记内容,二选一
		customCallout:{name:'需要的数据',display: 'BYCLICK'},// '自定义气泡窗口' - 自定义窗口内容 - 使用cover-view覆盖 ,二选一
	}
]

自定义气泡窗口参考:uniapp map自定义气泡窗

相关推荐
码兄科技13 小时前
24小时自助健身房系统软件开发实战指南:功能设计与部署方案
java·spring boot·uni-app
2501_9160074715 小时前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
2501_9160088916 小时前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
2501_9160088918 小时前
怎么用 Godot 导出 iOS 应用并上架 App Store?签名字段该填什么?
android·ios·小程序·https·uni-app·iphone·webview
郑州光合科技余经理1 天前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
白远山2 天前
城市电竞陪玩调度系统实战:从派单算法到多端协同的架构拆解
java·架构·uni-app·需求分析
白远山3 天前
上海24小时自助健身房系统软件开发实战指南
java·架构·uni-app·需求分析
白远山3 天前
上海24小时自助健身房系统软件开发实战指南:从需求到部署
java·架构·uni-app·需求分析
海鸥两三4 天前
小程序文档预览 · 进阶面试题
小程序·uni-app·uniapp
海鸥两三4 天前
微信小程序 PDF 预览实践(uni.downloadFile → uni.openDocument)
小程序·uni-app