经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序开发之实现一个跑步小程序
来源:jb51  时间:2022/8/23 15:36:53  对本文有异议

自己动手实现一个跑步小程序 用到的方法:wx.onLocationChange,监听实时地理位置变化事件,需结合 wx.startLocationUpdateBackgroundwx.startLocationUpdate使用。

地图组件

定义一个全屏的地图,地图配置项经纬度(longitudelatitude),缩放(scale),标记点(markers),路线(polyline)等

  1. <view class="map">
  2. <map id="map" longitude="{{longitude}}" latitude="{{latitude}}" scale="{{scale}}" markers="{{markers}}"
  3. polyline="{{polyline}}" style="width: 100%; height: 100%"></map>
  4. </view>

地图配置项字段

  1. data: {
  2. latitude: '',
  3. longitude: '',
  4. scale: 16,
  5. markers: [],
  6. polyline: [{
  7. points: [],
  8. color: '#FB8337',
  9. width: 5
  10. }]
  11. },

当前位置

wx.getLocation获取当前位置,

  1. // 获取当前位置
  2. getNowLocation() {
  3. wx.getLocation({
  4. type: 'gcj02',
  5. isHighAccuracy: true,
  6. success: (res) => {
  7. this.setData({
  8. latitude: res.latitude,
  9. longitude: res.longitude,
  10. })
  11. }
  12. })
  13. },

效果如图:

需在app.json中配置

  1. "permission": {
  2. "scope.userLocation": {
  3. "desc": "你的位置信息将用于小程序位置接口的效果展示"
  4. }
  5. },
  6. "requiredBackgroundModes": [
  7. "location"
  8. ],
  9. "requiredPrivateInfos": [
  10. "getLocation",
  11. "onLocationChange",
  12. "startLocationUpdate",
  13. "startLocationUpdateBackground"
  14. ]

效果如下:

点击允许,就可以看到当前位置了

开始跑步按钮

添加一个开始跑步按钮

  1. <button bindtap="startRun" class="btn" type="primary">开始跑步</button>
  1. .map {
  2. width: 100%;
  3. height: 100vh;
  4. }
  5.  
  6. .btn {
  7. position: absolute;
  8. bottom: 100rpx;
  9. left: 0;
  10. right: 0;
  11. z-index: 1000;
  12. }

GPS定位

点击开始跑步的时候,我们需要获取手机的GPS定位是否开启,开启后才能获取地图返回我们的坐标

  1. // 判断手机定位是否开启
  2. checkGPS() {
  3. wx.getSystemInfo({
  4. success: (res) => {
  5. if (!res.locationEnabled) {
  6. wx.showModal({
  7. title: '提示',
  8. content: '请先开启手机GPS定位',
  9. showCancel: false
  10. })
  11. return;
  12. }
  13. }
  14. })
  15. },

开发者工具获取不到,只能用手机测试

设置前后台允许获取定位

  1. wx.startLocationUpdate({
  2. success: () => {
  3. wx.onLocationChange((res) => {
  4. this.setData({
  5. latitude: res.latitude,
  6. longitude: res.longitude
  7. })
  8. wx.getSetting({
  9. success: (res) => {
  10. wx.hideLoading()
  11. if (!res.authSetting['scope.userLocationBackground']) {
  12. wx.showModal({
  13. title: '提示',
  14. content: '为确保后台定位精确,请先设置 "使用小程序时和离开后允许" 再进行跑步',
  15. showCancel: false,
  16. success: function (res) {
  17. if (res.confirm) {
  18. wx.openSetting()
  19. }
  20. }
  21. })
  22. } else {
  23. this.running();
  24. }
  25. }
  26. })
  27. wx.offLocationChange();
  28. wx.stopLocationUpdate();
  29. })
  30. },
  31. })

开启前后台定位

  1. // 开始前后台定位
  2. wx.startLocationUpdateBackground({
  3. success: () => {
  4. draw();
  5. time();
  6. },
  7. fail: () => {
  8. wx.showToast({
  9. title: '后台定位开启失败',
  10. icon: 'none'
  11. })
  12. }
  13. })

绘制路线

  1. let arr = this.data.polyline[0].points;
  2. wx.onLocationChange((res) => {
  3. if (dis > 0) {
  4. arr.push({
  5. latitude: res.latitude,
  6. longitude: res.longitude
  7. })
  8. totalDistance = Number(((totalDistance + dis) * 100).toFixed(2)) / 100;
  9. }
  10. }
  11. this.setData({
  12. 'polyline[0].points': arr
  13. })
  14. })

以上就是微信小程序开发之实现一个跑步小程序的详细内容,更多关于微信跑步小程序的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号