微信小程序開發(fā)之實現(xiàn)一個跑步小程序
自己動手實現(xiàn)一個跑步小程序 用到的方法:wx.onLocationChange,監(jiān)聽實時地理位置變化事件,需結(jié)合 wx.startLocationUpdateBackground
,wx.startLocationUpdate
使用。
地圖組件
定義一個全屏的地圖,地圖配置項經(jīng)緯度(longitude
,latitude
),縮放(scale
),標記點(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>
地圖配置項字段
data: { latitude: '', longitude: '', scale: 16, markers: [], polyline: [{ points: [], color: '#FB8337', width: 5 }] },
當前位置
用wx.getLocation
獲取當前位置,
// 獲取當前位置 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" ]
效果如下:
點擊允許,就可以看到當前位置了
開始跑步按鈕
添加一個開始跑步按鈕
<button bindtap="startRun" class="btn" type="primary">開始跑步</button>
.map { width: 100%; height: 100vh; } .btn { position: absolute; bottom: 100rpx; left: 0; right: 0; z-index: 1000; }
GPS定位
點擊開始跑步的時候,我們需要獲取手機的GPS定位是否開啟,開啟后才能獲取地圖返回我們的坐標
// 判斷手機定位是否開啟 checkGPS() { wx.getSystemInfo({ success: (res) => { if (!res.locationEnabled) { wx.showModal({ title: '提示', content: '請先開啟手機GPS定位', showCancel: false }) return; } } }) },
開發(fā)者工具獲取不到,只能用手機測試
設(shè)置前后臺允許獲取定位
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: '為確保后臺定位精確,請先設(shè)置 "使用小程序時和離開后允許" 再進行跑步', showCancel: false, success: function (res) { if (res.confirm) { wx.openSetting() } } }) } else { this.running(); } } }) wx.offLocationChange(); wx.stopLocationUpdate(); }) }, })
開啟前后臺定位
// 開始前后臺定位 wx.startLocationUpdateBackground({ success: () => { draw(); time(); }, fail: () => { wx.showToast({ title: '后臺定位開啟失敗', 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 }) })
以上就是微信小程序開發(fā)之實現(xiàn)一個跑步小程序的詳細內(nèi)容,更多關(guān)于微信跑步小程序的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
實現(xiàn)非常簡單的js雙向數(shù)據(jù)綁定
Angular實現(xiàn)了雙向綁定機制。所謂的雙向綁定,無非是從界面的操作能實時反映到數(shù)據(jù),數(shù)據(jù)的變更能實時展現(xiàn)到界面。本文給大家詳細介紹js雙向數(shù)據(jù)綁定,感興趣的朋友參考下2015-11-11node.js chat程序如何實現(xiàn)Ajax long-polling長鏈接刷新模式
node.js chat是node.js作者用JS寫的一個多人聊天工具, 源代碼公開下載,網(wǎng)址是chat.nodejs.org。作者用這個小例子,來展示如何用nodejs開發(fā)高效率的應(yīng)用程序。對于nodejs的學(xué)習者來說,是一個很好的例子2012-03-03等待指定時間后自動跳轉(zhuǎn)或關(guān)閉當前頁面的js代碼
本文為大家詳細介紹下如何通過js實現(xiàn)等待指定時間后自動跳轉(zhuǎn)或關(guān)閉當前頁面的腳步代碼,感興趣的朋友可以參考下哈,希望對大家有所幫助2013-07-07