地图圈系统——微信小程序(19)
一、查看轨迹及保存轨迹功能
在用户记录完轨迹之后可以查看自己的轨迹(即轨迹可以进行回放),如果觉得值得记录,则可以保存轨迹到后台。
界面会显示起点和终点,分别根据轨迹点数组的第一个元素和最后一个元素来获取的起点和终点。
轨迹是polyline来进行绘制。
保存轨迹需要上传用户名、用时、起点、终点、轨迹点数组。
二、项目代码
<!--pages/guiji_index/guiji_index.wxml-->
<navBar
title-text="查看轨迹"
back-icon="../../src/images/back@3x.png"
background="#f2f2f2"
bindback="back"/>
<view class="aa">
<!-- <map id="map" longitude="{{longitude}}" latitude="{{latitude}}" scale="16" markers="{{markers}}" polyline="{{polyline}}" show-location style="width: 100%; height: 100vh;">
</map> -->
<map id="map" class="map" longitude="{{longitude}}" latitude="{{latitude}}" scale="14" markers="{{markers}}" class='mapView' polyline='{{polyline}}' show-location='{{true}}' subkey="YDYBZ-QBSRX-M4X4R-Z6JWA-YECKH-GQFCY">
<cover-view class="map-prompt" bindtap="moveMarker">开始回放</cover-view>
<cover-view class="map-prompt1" bindtap="save">保存轨迹</cover-view>
<cover-view class="time"> \n\n耗时:{{costtime}}</cover-view>
<!-- <cover-view class="start"> \n\n起点:{{start}}</cover-view>
<cover-view class="end"> \n\n终点:{{end}}</cover-view> -->
</map>
</view>
<view class="box">
<view class="cu-bar bg-white">
<view class="action sub-title">
<text class="text-xl text-bold text-blue">起点:{{start}}</text>
<text class="text-ABC text-blue">Start point</text>
<!-- last-child选择器-->
</view>
</view>
<view class="cu-bar bg-white">
<view class="action sub-title">
<text class="text-xl text-bold text-blue">终点:{{end}}</text>
<text class="text-ABC text-blue">End point</text>
<!-- last-child选择器-->
</view>
</view>
</view>
<!-- <view>
<text>起点:{{start}}</text>
<text>终点:{{end}}</text>
</view> -->
/* pages/guiji_index/guiji_index.wxss *//* pages/router_index/router_index.wxss */
@import "../../colorui/main.wxss";
@import "../../colorui/icon.wxss";
.aa{
margin-bottom: 50rpx;
}
.map{
margin-top: 150rpx;
}
.map-prompt {
width: 220rpx;
height: 80rpx;
line-height: 80rpx;
font-size: 34rpx;
color: #fff;
text-align: center;
background: rgb(154, 155, 240);
border-radius: 40rpx;
box-shadow: 0 0 10rpx rgba(0,0,0,0.1);
position: absolute;
bottom: 150rpx;
left: 50%;
transform: translate(-50%,0);
}
.map-prompt1 {
width: 220rpx;
height: 80rpx;
line-height: 80rpx;
font-size: 34rpx;
color: #fff;
text-align: center;
background: rgb(154, 155, 240);
border-radius: 40rpx;
box-shadow: 0 0 10rpx rgba(0,0,0,0.1);
position: absolute;
bottom: 50rpx;
left: 50%;
transform: translate(-50%,0);
}
.mapView {
width: 100%;
height: 1210rpx;
}
.btn {
width: 40%;
height: 50px;
margin-top: 50rpx;
margin-left:30%;
margin-right:30%;
border-radius: 10px;
background-color: #4bc;
line-height: 50px;
text-align: center;
color: white;
}
.time{
background-color: #909399;
font-size: 40rpx;
padding-left: 90rpx;
padding-top: 20rpx;
color: #fff;
border-radius: 40rpx;
width: 50%;
height: 80rpx;
display: flex;
align-items: center;
justify-content: center;
position: absolute;
top: 50rpx;
left: 50%;
transform: translate(-50%);
}
.start{
background-color: #909399;
font-size: 33rpx;
padding-left: 90rpx;
padding-top: 20rpx;
color: #fff;
border-radius: 40rpx;
width: 90%;
height: 80rpx;
display: flex;
align-items: center;
justify-content: center;
position: absolute;
top: 50rpx;
left: 50%;
transform: translate(-50%);
}
.end{
background-color: #909399;
font-size: 33rpx;
padding-left: 90rpx;
padding-top: 20rpx;
color: #fff;
border-radius: 40rpx;
width: 90%;
height: 80rpx;
display: flex;
align-items: center;
justify-content: center;
position: absolute;
top: 150rpx;
left: 50%;
transform: translate(-50%);
}
// 全屏地图路线图并动画移动
// polyline中的points可以获取json用来绘制轨迹图
// 获取应用实例
var QQMapWX = require('../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var util = require('../../utils/util.js');
var qqmapsdk;
import { request } from "../../requests/index.js";
import {
CDN_PATH,
PLUGIN_KEY
} from '../../config/appConfig';
var btnEnable = true
Page({
data: {
id:'',
start:'',
end:'',
username:'',
costtime:'',
CDN_PATH,
point:[],
markers: [], // 标记点集合
polyline: [], // 坐标点集合
satellite: true, // 是否开启卫星图
// i: 0 // 用于循环
},
// onReady: function () {
// this.mapCtx = wx.createMapContext('map'); // 创建 map 上下文 MapContext 对象
// },
onReady: function () {
this.mapCtx = wx.createMapContext('map', this)
},
onLoad: function (options) {
console.log(options)
// var id=options.id
let that = this
that.setData({
costtime:options.costtime,
username:options.username,
point:JSON.parse(options.point)
})
var value = wx.getStorageSync('firstuser')
console.log(value)
// var result = await request({ url:'http://8.140.176.167:8800/router/findbyid/'+id });
// console.log(result)
let testPoints = JSON.parse(options.point)
qqmapsdk = new QQMapWX({
key: 'LE2BZ-ZIPKU-AWCVW-4EGZV-YAMY2-36BS4' // 必填
});
//2、根据坐标获取当前位置名称,显示在顶部:腾讯地图逆地址解析
qqmapsdk.reverseGeocoder({
location: {
latitude: testPoints[testPoints.length-1].latitude,
longitude: testPoints[testPoints.length-1].longitude
},
success: function (addressRes) {
var address = addressRes.result.formatted_addresses.recommend;
console.log(address)
that.setData({
end:address
})
}
})
qqmapsdk.reverseGeocoder({
location: {
latitude: testPoints[0].latitude,
longitude: testPoints[0].longitude
},
success: function (addressRes) {
var address = addressRes.result.formatted_addresses.recommend;
console.log(address)
that.setData({
start:address
})
}
})
var markers = []
var a = {
iconPath: value.avatarUrl,
id: 20,
latitude: testPoints[0].latitude,
longitude: testPoints[0].longitude,
width: 30,
height: 30,
i:'',
}
markers.push(a)
this.setData({
markers,
})
let newPolyline = ({
points: testPoints,
color: '#5085E7',
width: 5,
dottedLine: false
})
this.setData({
polyline: [newPolyline]
})
// var that = this
setTimeout(function () {
that.mapCtx.includePoints({
padding: [50, 50, 50, 50],
points: testPoints
})
}, 200)
},
/**测试点坐标运动 */
moveMarker: function () {
if (!btnEnable) {
return
}
var points = this.data.polyline[0].points
console.log(points)
this.loopAnamation(points,0,20)
btnEnable = false
},
/**
* 创建循环动画
*/
loopAnamation(subArray,index, markerId) {
var that = this
console.log(subArray)
console.log(index)
console.log(markerId)
if (index >= subArray.length) {
btnEnable = true
return
}
console.log('开始移动第', index, '个点', 'markerId:', markerId, subArray)
that.mapCtx.translateMarker({
markerId: 20,
autoRotate: false,
duration: 15000 / (subArray.length - 1),
destination: {
latitude: subArray[index].latitude,
longitude: subArray[index].longitude,
},
animationEnd() {
console.log('animation end')
that.loopAnamation(subArray, index + 1, markerId)
},
fail: function (e) {
console.log("来了老弟");
console.log(e);
}
})
},
onHide: function () {
clearInterval(this.data.i)
},
onUnload: function () {
clearInterval(this.data.i)
},
save(){
var that = this
var a = {
username:this.data.username,
use_time:this.data.costtime,
startpoint:this.data.start,
// startpoint:"历城区伯乐路",
endpoint:this.data.end,
// endpoint:"历城区舜泰北路",
router:JSON.stringify(this.data.point)
}
wx.request({
url: 'http://8.140.176.167:8800/router/addRouter',
method:'POST',
data:a,
success:function(res){
wx.showToast({
title: "保存成功",
duration: 1000,
icon: "sucess",
make: true
});
var i = setInterval(function() { //当前页面
wx.navigateBack({
delta: 1,
})
},1000)
that.setData({
i:i
})
}
})
}
})