微信小程序入門之指南針
微信小程序入門案例——指南針,供大家參考,具體內(nèi)容如下
涉及技術(shù):獲取地理位置、監(jiān)聽指南針角度
目錄結(jié)構(gòu):

pages\index\index.js
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
rotate:0,
degree:'未知',
direction:'',
lat:0,
lon:0,
alt:0
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
var that = this;
wx.getLocation({
altitude: true,
success:function(res){
that.setData({
lat:res.latitude.toFixed(2),
lon:res.longitude.toFixed(2),
alt:res.altitude.toFixed(2)
})
}
})
wx.onCompassChange(function(res){
let degree = res.direction.toFixed(0);
that.getDirection(degree)
that.setData({
rotate:360 - degree
})
})
},
/**
* 判斷方向
*/
getDirection:function(deg){
let dir = '未知';
if(deg>=340||deg<=20){
dir='北';
}else if(deg>20&°<70){
dir='東北';
}else if(deg>=70&°<=110){
dir='東';
}else if(deg>110&°<160){
dir='東南';
}else if(deg>=160&°<=200){
dir='南';
}else if(deg>200&°<250){
dir='西南';
}else if(deg>=250&°<=290){
dir='西';
}else if(deg>290&°<340){
dir='西北';
}
this.setData({
degree:deg,
direction:dir
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面卸載
*/
onUnload: function () {
},
/**
* 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作
*/
onPullDownRefresh: function () {
},
/**
* 頁面上拉觸底事件的處理函數(shù)
*/
onReachBottom: function () {
},
/**
* 用戶點擊右上角分享
*/
onShareAppMessage: function () {
}
})
pages\index\index.wxml
<view class="container">
<image src="/images/1.jpg" mode="widthFix" style="transform:rotate({{rotate}}deg);"></image>
<view class="status">
<text class="bigTxt">{{degree}}°{{direction}}</text>
<text class="smallTxt">北緯{{lat}}東經(jīng){{lon}}</text>
<text class="smallTxt">海拔{{alt}}米</text>
</view>
</view>
pages\index\index.wxss
.container{
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
color: #A46248;
}
image{
width: 80%;
}
.status{
display: flex;
flex-direction: column;
align-items: center;
}
.bigTxt{
font-size: 30pt;
margin: 15rpx;
}
.smallTxt{
font-size: 20pt;
margin: 15rpx;
}
app.js
App({
/**
* 當小程序初始化完成時,會觸發(fā) onLaunch(全局只觸發(fā)一次)
*/
onLaunch: function () {
},
/**
* 當小程序啟動,或從后臺進入前臺顯示,會觸發(fā) onShow
*/
onShow: function (options) {
},
/**
* 當小程序從前臺進入后臺,會觸發(fā) onHide
*/
onHide: function () {
},
/**
* 當小程序發(fā)生腳本錯誤,或者 api 調(diào)用失敗時,會觸發(fā) onError 并帶上錯誤信息
*/
onError: function (msg) {
}
})
app.json
{
"pages":[
"pages/index/index"
],
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "指南針",
"navigationBarTextStyle":"black"
},
"permission":{
"scope.userLocation":{
"desc":"你的位置信息將用于小程序指南針的效果展示"
}
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
運行截圖:

為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
uniapp中全局頁面掛載組件實戰(zhàn)過程(小程序)
這篇文章主要給大家介紹了關(guān)于uniapp中全局頁面掛載組件(小程序)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用uniapp具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-12-12
JS+CSS實現(xiàn)超漂亮的動態(tài)翻書效果(思路詳解)
我們平常沖浪時是不是看過一些學(xué)校高級的錄取通知書,翻開通知書就能看見里面的內(nèi)容,呈現(xiàn)出逼真的3D效果,本文帶領(lǐng)大家基于JS+CSS實現(xiàn)超漂亮的動態(tài)翻書效果,需要的朋友可以參考下2023-05-05
trackingjs+websocket+百度人臉識別API實現(xiàn)人臉簽到
這篇文章主要介為大家詳細紹了trackingjs+websocket+百度人臉識別API實現(xiàn)人臉簽到功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
用javascript實現(xiàn)的支持lrc歌詞的播放器
用javascript實現(xiàn)的支持lrc歌詞的播放器...2007-05-05

