微信小程序入门案例——指南针

浅浅的花香味﹌ 2022-12-14 03:59 379阅读 0赞

微信小程序入门案例——指南针

涉及技术:获取地理位置、监听指南针角度

目录结构:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l1bmZlYXRoZXI_size_16_color_FFFFFF_t_70

pages\index\index.js

  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. rotate:0,
  7. degree:'未知',
  8. direction:'',
  9. lat:0,
  10. lon:0,
  11. alt:0
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad: function (options) {
  17. var that = this;
  18. wx.getLocation({
  19. altitude: true,
  20. success:function(res){
  21. that.setData({
  22. lat:res.latitude.toFixed(2),
  23. lon:res.longitude.toFixed(2),
  24. alt:res.altitude.toFixed(2)
  25. })
  26. }
  27. })
  28. wx.onCompassChange(function(res){
  29. let degree = res.direction.toFixed(0);
  30. that.getDirection(degree)
  31. that.setData({
  32. rotate:360 - degree
  33. })
  34. })
  35. },
  36. /**
  37. * 判断方向
  38. */
  39. getDirection:function(deg){
  40. let dir = '未知';
  41. if(deg>=340||deg<=20){
  42. dir='北';
  43. }else if(deg>20&&deg<70){
  44. dir='东北';
  45. }else if(deg>=70&&deg<=110){
  46. dir='东';
  47. }else if(deg>110&&deg<160){
  48. dir='东南';
  49. }else if(deg>=160&&deg<=200){
  50. dir='南';
  51. }else if(deg>200&&deg<250){
  52. dir='西南';
  53. }else if(deg>=250&&deg<=290){
  54. dir='西';
  55. }else if(deg>290&&deg<340){
  56. dir='西北';
  57. }
  58. this.setData({
  59. degree:deg,
  60. direction:dir
  61. })
  62. },
  63. /**
  64. * 生命周期函数--监听页面初次渲染完成
  65. */
  66. onReady: function () {
  67. },
  68. /**
  69. * 生命周期函数--监听页面显示
  70. */
  71. onShow: function () {
  72. },
  73. /**
  74. * 生命周期函数--监听页面隐藏
  75. */
  76. onHide: function () {
  77. },
  78. /**
  79. * 生命周期函数--监听页面卸载
  80. */
  81. onUnload: function () {
  82. },
  83. /**
  84. * 页面相关事件处理函数--监听用户下拉动作
  85. */
  86. onPullDownRefresh: function () {
  87. },
  88. /**
  89. * 页面上拉触底事件的处理函数
  90. */
  91. onReachBottom: function () {
  92. },
  93. /**
  94. * 用户点击右上角分享
  95. */
  96. onShareAppMessage: function () {
  97. }
  98. })

pages\index\index.wxml

  1. <view class="container">
  2. <image src="/images/1.jpg" mode="widthFix" style="transform:rotate({
  3. {rotate}}deg);"></image>
  4. <view class="status">
  5. <text class="bigTxt">{
  6. {degree}}°{
  7. {direction}}</text>
  8. <text class="smallTxt">北纬{
  9. {lat}}东经{
  10. {lon}}</text>
  11. <text class="smallTxt">海拔{
  12. {alt}}米</text>
  13. </view>
  14. </view>

pages\index\index.wxss

  1. .container{
  2. height: 100vh;
  3. display: flex;
  4. flex-direction: column;
  5. align-items: center;
  6. justify-content: space-around;
  7. color: #A46248;
  8. }
  9. image{
  10. width: 80%;
  11. }
  12. .status{
  13. display: flex;
  14. flex-direction: column;
  15. align-items: center;
  16. }
  17. .bigTxt{
  18. font-size: 30pt;
  19. margin: 15rpx;
  20. }
  21. .smallTxt{
  22. font-size: 20pt;
  23. margin: 15rpx;
  24. }

app.js

  1. App({
  2. /**
  3. * 当小程序初始化完成时,会触发 onLaunch(全局只触发一次)
  4. */
  5. onLaunch: function () {
  6. },
  7. /**
  8. * 当小程序启动,或从后台进入前台显示,会触发 onShow
  9. */
  10. onShow: function (options) {
  11. },
  12. /**
  13. * 当小程序从前台进入后台,会触发 onHide
  14. */
  15. onHide: function () {
  16. },
  17. /**
  18. * 当小程序发生脚本错误,或者 api 调用失败时,会触发 onError 并带上错误信息
  19. */
  20. onError: function (msg) {
  21. }
  22. })

app.json

  1. {
  2. "pages":[
  3. "pages/index/index"
  4. ],
  5. "window":{
  6. "backgroundTextStyle":"light",
  7. "navigationBarBackgroundColor": "#fff",
  8. "navigationBarTitleText": "指南针",
  9. "navigationBarTextStyle":"black"
  10. },
  11. "permission":{
  12. "scope.userLocation":{
  13. "desc":"你的位置信息将用于小程序指南针的效果展示"
  14. }
  15. },
  16. "style": "v2",
  17. "sitemapLocation": "sitemap.json"
  18. }

运行截图:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l1bmZlYXRoZXI_size_16_color_FFFFFF_t_70 1

发表评论

表情:
评论列表 (有 0 条评论,379人围观)

还没有评论,来说两句吧...

相关阅读

    相关 程序入门

    开发工具与关键技术: 作者:肖广斌 撰写时间:2021年1月27日 微信小程序,简称小程序,是一种不需要安装便可以使用的应用,它实现了应用“触手可及”的梦想,用户扫一

    相关 程序入门

    了解小程序 什么是小程序?  小程序其实就是一个工具,为人提供便捷服务的;随时可用,用完即走,减少桌面上面的app数量; B2C(人与商品) 淘宝 JD  P2P(