微信小程序開(kāi)發(fā)之實(shí)現(xiàn)一個(gè)跑步小程序
自己動(dòng)手實(shí)現(xiàn)一個(gè)跑步小程序 用到的方法:wx.onLocationChange,監(jiān)聽(tīng)實(shí)時(shí)地理位置變化事件,需結(jié)合 wx.startLocationUpdateBackground,wx.startLocationUpdate使用。
地圖組件
定義一個(gè)全屏的地圖,地圖配置項(xiàng)經(jīng)緯度(longitude,latitude),縮放(scale),標(biāo)記點(diǎn)(markers),路線(polyline)等
<view class="map">
<map id="map" longitude="{{longitude}}" latitude="{{latitude}}" scale="{{scale}}" markers="{{markers}}"
polyline="{{polyline}}" style="width: 100%; height: 100%"></map>
</view>地圖配置項(xiàng)字段
data: {
latitude: '',
longitude: '',
scale: 16,
markers: [],
polyline: [{
points: [],
color: '#FB8337',
width: 5
}]
},當(dāng)前位置
用wx.getLocation獲取當(dāng)前位置,
// 獲取當(dāng)前位置
getNowLocation() {
wx.getLocation({
type: 'gcj02',
isHighAccuracy: true,
success: (res) => {
this.setData({
latitude: res.latitude,
longitude: res.longitude,
})
}
})
},效果如圖:

需在app.json中配置
"permission": {
"scope.userLocation": {
"desc": "你的位置信息將用于小程序位置接口的效果展示"
}
},
"requiredBackgroundModes": [
"location"
],
"requiredPrivateInfos": [
"getLocation",
"onLocationChange",
"startLocationUpdate",
"startLocationUpdateBackground"
]效果如下:

點(diǎn)擊允許,就可以看到當(dāng)前位置了

開(kāi)始跑步按鈕
添加一個(gè)開(kāi)始跑步按鈕
<button bindtap="startRun" class="btn" type="primary">開(kāi)始跑步</button>
.map {
width: 100%;
height: 100vh;
}
.btn {
position: absolute;
bottom: 100rpx;
left: 0;
right: 0;
z-index: 1000;
}GPS定位
點(diǎn)擊開(kāi)始跑步的時(shí)候,我們需要獲取手機(jī)的GPS定位是否開(kāi)啟,開(kāi)啟后才能獲取地圖返回我們的坐標(biāo)
// 判斷手機(jī)定位是否開(kāi)啟
checkGPS() {
wx.getSystemInfo({
success: (res) => {
if (!res.locationEnabled) {
wx.showModal({
title: '提示',
content: '請(qǐng)先開(kāi)啟手機(jī)GPS定位',
showCancel: false
})
return;
}
}
})
},開(kāi)發(fā)者工具獲取不到,只能用手機(jī)測(cè)試

設(shè)置前后臺(tái)允許獲取定位
wx.startLocationUpdate({
success: () => {
wx.onLocationChange((res) => {
this.setData({
latitude: res.latitude,
longitude: res.longitude
})
wx.getSetting({
success: (res) => {
wx.hideLoading()
if (!res.authSetting['scope.userLocationBackground']) {
wx.showModal({
title: '提示',
content: '為確保后臺(tái)定位精確,請(qǐng)先設(shè)置 "使用小程序時(shí)和離開(kāi)后允許" 再進(jìn)行跑步',
showCancel: false,
success: function (res) {
if (res.confirm) {
wx.openSetting()
}
}
})
} else {
this.running();
}
}
})
wx.offLocationChange();
wx.stopLocationUpdate();
})
},
})開(kāi)啟前后臺(tái)定位
// 開(kāi)始前后臺(tái)定位
wx.startLocationUpdateBackground({
success: () => {
draw();
time();
},
fail: () => {
wx.showToast({
title: '后臺(tái)定位開(kāi)啟失敗',
icon: 'none'
})
}
})繪制路線
let arr = this.data.polyline[0].points;
wx.onLocationChange((res) => {
if (dis > 0) {
arr.push({
latitude: res.latitude,
longitude: res.longitude
})
totalDistance = Number(((totalDistance + dis) * 100).toFixed(2)) / 100;
}
}
this.setData({
'polyline[0].points': arr
})
})以上就是微信小程序開(kāi)發(fā)之實(shí)現(xiàn)一個(gè)跑步小程序的詳細(xì)內(nèi)容,更多關(guān)于微信跑步小程序的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 微信小程序開(kāi)發(fā)之實(shí)現(xiàn)食堂點(diǎn)餐系統(tǒng)
- 微信小程序開(kāi)發(fā)之實(shí)現(xiàn)記賬本
- 微信小程序開(kāi)發(fā)之實(shí)現(xiàn)心情記事本
- 微信小程序組件化開(kāi)發(fā)的實(shí)戰(zhàn)步驟
- 微信小程序開(kāi)發(fā)WXML模板語(yǔ)法基礎(chǔ)教程
- 微信小程序云開(kāi)發(fā)實(shí)現(xiàn)分頁(yè)刷新獲取數(shù)據(jù)
- 微信小程序開(kāi)發(fā)中組件的生命周期詳細(xì)介紹
- 微信小程序開(kāi)發(fā)中所碰到問(wèn)題集錦
相關(guān)文章
實(shí)現(xiàn)非常簡(jiǎn)單的js雙向數(shù)據(jù)綁定
Angular實(shí)現(xiàn)了雙向綁定機(jī)制。所謂的雙向綁定,無(wú)非是從界面的操作能實(shí)時(shí)反映到數(shù)據(jù),數(shù)據(jù)的變更能實(shí)時(shí)展現(xiàn)到界面。本文給大家詳細(xì)介紹js雙向數(shù)據(jù)綁定,感興趣的朋友參考下2015-11-11
CocosCreator通用框架設(shè)計(jì)之資源管理
這篇文章主要介紹了CocosCreator通用框架設(shè)計(jì)之資源管理,對(duì)性能優(yōu)化感興趣的同學(xué),一定要看一下2021-04-04
JavaScript實(shí)現(xiàn)LRU算法的示例詳解
不知道屏幕前的朋友們,有沒(méi)有和我一樣,覺(jué)得LRU算法原理很容易理解,實(shí)現(xiàn)起來(lái)卻很復(fù)雜。所以本文就為大家整理了一下實(shí)現(xiàn)的示例代碼,需要的可以參考一下2023-04-04
node.js chat程序如何實(shí)現(xiàn)Ajax long-polling長(zhǎng)鏈接刷新模式
node.js chat是node.js作者用JS寫(xiě)的一個(gè)多人聊天工具, 源代碼公開(kāi)下載,網(wǎng)址是chat.nodejs.org。作者用這個(gè)小例子,來(lái)展示如何用nodejs開(kāi)發(fā)高效率的應(yīng)用程序。對(duì)于nodejs的學(xué)習(xí)者來(lái)說(shuō),是一個(gè)很好的例子2012-03-03
JS實(shí)現(xiàn)微信里判斷頁(yè)面是否被分享成功的方法
這篇文章主要介紹了JS實(shí)現(xiàn)微信里判斷頁(yè)面是否被分享成功的方法,結(jié)合實(shí)例形式分析了js調(diào)用微信接口判斷網(wǎng)頁(yè)分享功能的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
等待指定時(shí)間后自動(dòng)跳轉(zhuǎn)或關(guān)閉當(dāng)前頁(yè)面的js代碼
本文為大家詳細(xì)介紹下如何通過(guò)js實(shí)現(xiàn)等待指定時(shí)間后自動(dòng)跳轉(zhuǎn)或關(guān)閉當(dāng)前頁(yè)面的腳步代碼,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07
解決JS表單驗(yàn)證只有第一個(gè)IF起作用的問(wèn)題
這篇文章主要介紹了解決JS表單驗(yàn)證只有第一個(gè)IF起作用的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12

