“案例: 运动打卡:跑步定位”的版本间的差异
来自CloudWiki
2513177689(讨论 | 贡献) (创建页面,内容为“# wsx ``` var filter ={ toFix:function(value){ return value.toFixed(2) // 保留2位小数 } } module.exports={ toFix: filter.toFix } ``` # js ``` var…”) |
2513177689(讨论 | 贡献) |
||
第1行: | 第1行: | ||
− | + | <h1>wsx</h1> | |
− | + | <pre><code>var filter ={ | |
− | |||
− | var filter ={ | ||
toFix:function(value){ | toFix:function(value){ | ||
return value.toFixed(2) // 保留2位小数 | return value.toFixed(2) // 保留2位小数 | ||
第11行: | 第9行: | ||
toFix: filter.toFix | toFix: filter.toFix | ||
} | } | ||
− | + | </code></pre> | |
− | + | <h1>js</h1> | |
− | + | <pre><code>var QQMapWX = require('../../libs/qqmap-wx-jssdk.js'); | |
− | |||
− | |||
− | var QQMapWX = require( | ||
var qqmapsdk = new QQMapWX({ | var qqmapsdk = new QQMapWX({ | ||
− | key: | + | key: 'IWLBZ-CK6R4-YXDUO-DQCXK-J5H4Z-DSBCI' |
}); | }); | ||
var app = getApp(); | var app = getApp(); | ||
第37行: | 第32行: | ||
time: app.globalData.time, | time: app.globalData.time, | ||
markers: [{ | markers: [{ | ||
− | iconPath: | + | iconPath: "../../imgs/pic.jpg", |
id: 0, | id: 0, | ||
latitude: 39.9205, | latitude: 39.9205, | ||
第50行: | 第45行: | ||
var that = this | var that = this | ||
wx.getLocation({ | wx.getLocation({ | ||
− | type: | + | type: 'gcj02', |
success(res) { | success(res) { | ||
that.setData({ | that.setData({ | ||
第71行: | 第66行: | ||
target_lo: res.longitude, | target_lo: res.longitude, | ||
markers: [{ | markers: [{ | ||
− | iconPath: | + | iconPath: "../../imgs/pic.jpg", |
id: 0, | id: 0, | ||
latitude: 39.9205, | latitude: 39.9205, | ||
第78行: | 第73行: | ||
height: 30 | height: 30 | ||
}, { | }, { | ||
− | iconPath: | + | iconPath: "../../imgs/blue.png", |
id: 1, | id: 1, | ||
latitude: res.latitude, | latitude: res.latitude, | ||
第91行: | 第86行: | ||
routing() { | routing() { | ||
var that = this | var that = this | ||
− | var temp_loc = this.data.target_la + | + | var temp_loc = this.data.target_la + ',' + this.data.target_lo |
qqmapsdk.direction({ | qqmapsdk.direction({ | ||
− | mode: | + | mode: 'walking', |
to: temp_loc, | to: temp_loc, | ||
success: function (res) { | success: function (res) { | ||
第101行: | 第96行: | ||
//坐标解压(返回的点串坐标,通过前向差分进行压缩) | //坐标解压(返回的点串坐标,通过前向差分进行压缩) | ||
var kr = 1000000; | var kr = 1000000; | ||
− | for (var i = 2; i | + | for (var i = 2; i < coors.length; i++) { |
coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr; | coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr; | ||
} | } | ||
//将解压后的坐标放入点串数组pl中 | //将解压后的坐标放入点串数组pl中 | ||
− | for (var i = 0; i | + | for (var i = 0; i < coors.length; i += 2) { |
pl.push({ latitude: coors[i], longitude: coors[i + 1] }) | pl.push({ latitude: coors[i], longitude: coors[i + 1] }) | ||
} | } | ||
第115行: | 第110行: | ||
polyline: [{ | polyline: [{ | ||
points: pl, | points: pl, | ||
− | color: | + | color: '#FF0000DD', |
width: 4 | width: 4 | ||
}] | }] | ||
第127行: | 第122行: | ||
app.globalData.begin_la = this.data.latitude | app.globalData.begin_la = this.data.latitude | ||
app.globalData.begin_lo = this.data.longitude | app.globalData.begin_lo = this.data.longitude | ||
− | app.globalData.interval = setInterval(() = | + | app.globalData.interval = setInterval(() => { |
this.running() | this.running() | ||
this.runPath() | this.runPath() | ||
第140行: | 第135行: | ||
time: app.globalData.time, | time: app.globalData.time, | ||
}) | }) | ||
− | app.globalData.interval = setInterval(() = | + | app.globalData.interval = setInterval(() => { |
this.running() | this.running() | ||
this.runPath() | this.runPath() | ||
第148行: | 第143行: | ||
clearInterval(app.globalData.interval) | clearInterval(app.globalData.interval) | ||
this.geoInfo() | this.geoInfo() | ||
− | console.log( | + | console.log('开始位置', app.globalData.begin_la.begin_la, app.globalData.begin_la.begin_lo) |
− | console.log( | + | console.log('结束位置', this.data.latitude, this.data.longitude) |
}, | }, | ||
clearRun(){ | clearRun(){ | ||
− | console.log( | + | console.log('数据已清除') |
app.globalData={} | app.globalData={} | ||
}, | }, | ||
第173行: | 第168行: | ||
polyline: [{ | polyline: [{ | ||
points: app.globalData.path, | points: app.globalData.path, | ||
− | color: | + | color: '#FF0000DD', |
width: 4 | width: 4 | ||
}], | }], | ||
markers: [{ | markers: [{ | ||
− | iconPath: | + | iconPath: "../../imgs/triangle.png", |
id: 3, | id: 3, | ||
latitude: app.globalData.begin_la, | latitude: app.globalData.begin_la, | ||
第184行: | 第179行: | ||
height: 20 | height: 20 | ||
}, { | }, { | ||
− | iconPath: | + | iconPath: "../../imgs/yellow.png", |
id: 4, | id: 4, | ||
latitude: this.data.latitude, | latitude: this.data.latitude, | ||
第196行: | 第191行: | ||
var that = this | var that = this | ||
wx.getLocation({ | wx.getLocation({ | ||
− | type: | + | type: 'gcj02', |
success(res) { | success(res) { | ||
that.setData({ | that.setData({ | ||
第212行: | 第207行: | ||
} | } | ||
}) | }) | ||
− | + | </code></pre> | |
− | + | <h1>json</h1> | |
− | + | <pre><code>{ | |
− | + | "usingComponents": {} | |
− | |||
− | { | ||
− | |||
} | } | ||
− | + | </code></pre> | |
− | + | <h1>wxml</h1> | |
− | + | <pre><code><wxs module='filter' src='fix.wxs'></wxs> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | <view class='mainView'> | |
− | + | <view>路线规划</view> | |
− | + | <view class='top-buttons'> | |
− | + | <button class='top-btn' bindtap='pickLocation'>选择位置</button> | |
− | + | <button class='top-btn' bindtap='routing'>显示路线</button> | |
− | + | </view> | |
− | |||
− | |||
− | |||
− | # | + | <map style="width:100%; height:350px" longitude='{{longitude}}' latitude='{{latitude}}' markers='{{markers}}' polyline="{{polyline}}"> |
+ | <cover-view class="cover"> | ||
+ | <cover-view>跑步距离:{{filter.toFix(meters)}} </cover-view> | ||
+ | <cover-view>跑步时长:{{filter.toFix(time)}}</cover-view> | ||
+ | </cover-view> | ||
+ | </map> | ||
− | + | <view>跑步定位</view> | |
− | .mainView { | + | <view class='bottom-buttons'> |
+ | <button class='bottom-btn' bindtap='startRun'>跑步开始</button> | ||
+ | <button class="bottom-btn" bindtap="continueRun">继续跑步</button> | ||
+ | <button class="bottom-btn" bindtap="stopRun">结束跑步</button> | ||
+ | <button class="bottom-btn" bindtap="clearRun">清除路线</button> | ||
+ | </view> | ||
+ | </view> | ||
+ | </code></pre> | ||
+ | <h1>wxss</h1> | ||
+ | <pre><code>.mainView { | ||
background-color: #5eb2df; | background-color: #5eb2df; | ||
color: #fff; | color: #fff; | ||
第292行: | 第278行: | ||
} | } | ||
− | + | </code></pre> | |
+ | <p> </p> |
2020年5月20日 (三) 08:40的最新版本
wsx
<code>var filter ={ toFix:function(value){ return value.toFixed(2) // 保留2位小数 } } module.exports={ toFix: filter.toFix } </code>
js
<code>var QQMapWX = require('../../libs/qqmap-wx-jssdk.js'); var qqmapsdk = new QQMapWX({ key: 'IWLBZ-CK6R4-YXDUO-DQCXK-J5H4Z-DSBCI' }); var app = getApp(); Page({ /** * 页面的初始数据 */ data: { latitude: 0, longitude: 0, target_la: 0, target_lo: 0, speed: 0, accuracy: 0, meters: app.globalData.meters, time: app.globalData.time, markers: [{ iconPath: "../../imgs/pic.jpg", id: 0, latitude: 39.9205, longitude: 116.4605, width: 30, height: 30 }], path: [], }, geoInfo() { var that = this wx.getLocation({ type: 'gcj02', success(res) { that.setData({ latitude: res.latitude, longitude: res.longitude, speed: res.speed, accuracy: res.accuracy, }) } }) console.log(this.data.latitude, this.data.longitude) }, pickLocation() { var that = this wx.chooseLocation({ success: function (res) { console.log(res) that.setData({ target_la: res.latitude, target_lo: res.longitude, markers: [{ iconPath: "../../imgs/pic.jpg", id: 0, latitude: 39.9205, longitude: 116.4605, width: 30, height: 30 }, { iconPath: "../../imgs/blue.png", id: 1, latitude: res.latitude, longitude: res.longitude, width: 20, height: 20 }] }) }, }) }, routing() { var that = this var temp_loc = this.data.target_la + ',' + this.data.target_lo qqmapsdk.direction({ mode: 'walking', to: temp_loc, success: function (res) { console.log(res) var ret = res; var coors = ret.result.routes[0].polyline, pl = []; //坐标解压(返回的点串坐标,通过前向差分进行压缩) var kr = 1000000; for (var i = 2; i < coors.length; i++) { coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr; } //将解压后的坐标放入点串数组pl中 for (var i = 0; i < coors.length; i += 2) { pl.push({ latitude: coors[i], longitude: coors[i + 1] }) } console.log(pl) //设置polyline属性,将路线显示出来,将解压坐标第一个数据作为起点 that.setData({ latitude: pl[0].latitude, longitude: pl[0].longitude, polyline: [{ points: pl, color: '#FF0000DD', width: 4 }] }) } }) }, startRun() { clearInterval(app.globalData.interval) this.geoInfo() app.globalData.begin_la = this.data.latitude app.globalData.begin_lo = this.data.longitude app.globalData.interval = setInterval(() => { this.running() this.runPath() }, 1000) }, continueRun(){ clearInterval(app.globalData.interval) this.setData({ latitude: app.globalData.latitude, longitude: app.globalData.longitude, meters: app.globalData.meters, time: app.globalData.time, }) app.globalData.interval = setInterval(() => { this.running() this.runPath() }, 1000) }, stopRun() { clearInterval(app.globalData.interval) this.geoInfo() console.log('开始位置', app.globalData.begin_la.begin_la, app.globalData.begin_la.begin_lo) console.log('结束位置', this.data.latitude, this.data.longitude) }, clearRun(){ console.log('数据已清除') app.globalData={} }, running() { this.setData({ latitude: this.data.latitude + 0.0002, longitude: this.data.longitude + 0.0002, meters: this.data.meters + 0.05, time: this.data.time + 1, }) app.globalData.latitude = this.data.latitude app.globalData.longitude = this.data.longitude app.globalData.meters = this.data.meters app.globalData.time = this.data.time console.log(this.data) }, runPath() { app.globalData.path.push({ latitude: this.data.latitude, longitude: this.data.longitude }) this.setData({ polyline: [{ points: app.globalData.path, color: '#FF0000DD', width: 4 }], markers: [{ iconPath: "../../imgs/triangle.png", id: 3, latitude: app.globalData.begin_la, longitude: app.globalData.begin_lo, width: 20, height: 20 }, { iconPath: "../../imgs/yellow.png", id: 4, latitude: this.data.latitude, longitude: this.data.longitude, width: 20, height: 20 }] }) }, onReady: function () { var that = this wx.getLocation({ type: 'gcj02', success(res) { that.setData({ latitude: res.latitude, longitude: res.longitude, speed: res.speed, accuracy: res.accuracy, }) } }) }, onLoad: function () { } }) </code>
json
<code>{ "usingComponents": {} } </code>
wxml
<code><wxs module='filter' src='fix.wxs'></wxs> <view class='mainView'> <view>路线规划</view> <view class='top-buttons'> <button class='top-btn' bindtap='pickLocation'>选择位置</button> <button class='top-btn' bindtap='routing'>显示路线</button> </view> <map style="width:100%; height:350px" longitude='{{longitude}}' latitude='{{latitude}}' markers='{{markers}}' polyline="{{polyline}}"> <cover-view class="cover"> <cover-view>跑步距离:{{filter.toFix(meters)}} </cover-view> <cover-view>跑步时长:{{filter.toFix(time)}}</cover-view> </cover-view> </map> <view>跑步定位</view> <view class='bottom-buttons'> <button class='bottom-btn' bindtap='startRun'>跑步开始</button> <button class="bottom-btn" bindtap="continueRun">继续跑步</button> <button class="bottom-btn" bindtap="stopRun">结束跑步</button> <button class="bottom-btn" bindtap="clearRun">清除路线</button> </view> </view> </code>
wxss
<code>.mainView { background-color: #5eb2df; color: #fff; text-align: center; } .top-buttons { display: flex; flex-direction: row; } .top-btn { margin-top: 10rpx; width: 300rpx; margin-bottom: 10rpx; font-size: 30rpx; align-items: center; } .cover{ color: blue; text-align: left; } .bottom-buttons { display: flex; flex-direction: row; flex-wrap: wrap; } .bottom-btn { margin-top: 10rpx; width: 300rpx; margin-bottom: 10rpx; font-size: 30rpx; align-items: center; } </code>