溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

微信小程序開發怎么實現一個跑步小程序

發布時間:2022-08-23 16:12:04 來源:億速云 閱讀:289 作者:iii 欄目:開發技術

這篇“微信小程序開發怎么實現一個跑步小程序”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“微信小程序開發怎么實現一個跑步小程序”文章吧。

自己動手實現一個跑步小程序 用到的方法:wx.onLocationChange,監聽實時地理位置變化事件,需結合 wx.startLocationUpdateBackground,wx.startLocationUpdate使用。

地圖組件

定義一個全屏的地圖,地圖配置項經緯度(longitude,latitude),縮放(scale),標記點(markers),路線(polyline)等

<view class="map">
    <map id="map" longitude="{{longitude}}" latitude="{{latitude}}" scale="{{scale}}" markers="{{markers}}"
        polyline="{{polyline}}" ></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;
            }
        }
    })
},

開發者工具獲取不到,只能用手機測試

微信小程序開發怎么實現一個跑步小程序

設置前后臺允許獲取定位

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: '為確保后臺定位精確,請先設置 "使用小程序時和離開后允許" 再進行跑步',
                            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
    })
})

以上就是關于“微信小程序開發怎么實現一個跑步小程序”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女