首頁 > web前端 > Uni-app > 主體

uniapp實現定位簽到

PHPz
發布: 2023-05-22 10:42:37
原創
1192 人瀏覽過

隨著行動網路的普及,許多企業都有了自己的行動應用,其中一個非常實用的功能就是定位簽到。透過定位簽到,企業可以實現對員工的管理,如考勤、任務分配等。本文介紹如何使用uniapp開發一個定位簽到的行動應用程式。

一、準備工作

在開始開發之前,需要準備好以下內容:

  1. uniapp開發環境
  2. 小程式開發工具
  3. 高德地圖開發者帳號

如果沒有相關經驗,可以先學習uniapp和小程式的基礎。接下來,進入正題。

二、整合高德地圖

  1. 註冊高德地圖開發者帳號

在高德地圖開放平台註冊開發者帳號,並創建應用取得Key。 Key是API呼叫的身份認證,可以在應用程式中使用。

  1. 整合高德地圖SDK

在uniapp專案中引入高德地圖SDK,方法如下:

1)在HBuilderX中開啟uniapp項目
2)右鍵點擊“uni_modules”資料夾,選擇“安裝npm依賴”
3)在搜尋框中輸入“@jv-uni/amap”,選擇“uni-app amap定位插件”,點擊“安裝」

  1. 實現授權及定位

在uniapp專案中實現授權及定位,具體步驟如下:

1)在頁面中使用以下程式碼引入高德地圖外掛程式

import amap from '@jv-uni/amap';
登入後複製

2)在需要進行定位的頁面中加入AMap.plugin方法

mounted() { 
  this.getLocation(); 
},
methods: { 
  getLocation() { 
    AMap.plugin('AMap.Geolocation', () => { 
      let geolocation = new AMap.Geolocation({ 
        enableHighAccuracy: true, 
        timeout: 10000, 
        buttonOffset: new AMap.Pixel(10, 10), 
        zoomToAccuracy: true, 
        buttonPosition: 'RB' 
      }); 
      geolocation.getCurrentPosition((status, result) => { 
        if (status === 'complete') { 
          this.longitude = result.position.lng; 
          this.latitude = result.position.lat; 
          this.address = result.formattedAddress; 
        } else { 
          uni.showToast({ 
            icon: 'none', 
            title: '获取地址失败' 
          }); 
        } 
      }); 
    }); 
  } 
}
登入後複製

透過AMap.plugin方法,我們引入了高德地圖定位插件,同時獲取了當前設備的經緯度和地址資訊。

三、實現簽到功能

透過上面的步驟,我們已經可以獲取到當前位置信息,接下來就可以根據獲取到的位置信息實現簽到功能。

  1. 儲存簽到位置資訊

在取得到位置資訊後,我們需要將這些資訊儲存到資料庫中。可以透過呼叫uniapp中的資料儲存API實現儲存功能,具體步驟如下:

uni.setStorageSync('longitude', this.longitude); 
uni.setStorageSync('latitude', this.latitude); 
uni.setStorageSync('address', this.address); 
登入後複製
  1. 顯示簽到狀態

待簽到位置資訊儲存成功後,顯示簽到狀態。我們可以在目前頁面上設定一個簽到按鈕,在使用者點擊該按鈕後,顯示簽到結果。

 
签到成功 
未签到 
登入後複製

透過v-if指令,實現簽到成功後的顯示效果。

  1. 簽到規則

簽到功能的實作還需要考慮簽到規則。企業的簽到規則一般包括簽到時間、簽到地址等。可以透過以下步驟簡單實作簽到規則:

1)記錄目前時間

我們可以在簽到按鈕中新增一個取得目前時間的方法。

getNowFormatDate() { 
  let date = new Date(); 
  let seperator1 = "-"; 
  let year = date.getFullYear(); 
  let month = date.getMonth() + 1; 
  let strDate = date.getDate(); 
  if (month >= 1 && month <= 9) { 
    month = "0" + month; 
  } 
  if (strDate >= 0 && strDate <= 9) { 
    strDate = "0" + strDate; 
  } 
  let currentdate = year + seperator1 + month + seperator1 + strDate; 
  return currentdate; 
}
登入後複製

2)定義簽到規則

簽到規則需要包含簽到時間、簽到位址等,我們可以在uniapp專案中設定一個JSON物件以實作儲存簽到規則。

signs: { 
  "2021-11-01": [ 
    { 
      longitude: 116.397428, 
      latitude: 39.90923, 
      address: "北京市东城区正义路5号" 
    }, 
    ... 
  ], 
  ... 
} 
登入後複製

其中,「2021-11-01」表示某一天的簽到規則,其值為一個陣列。數組中以JSON物件的形式儲存了簽到地點的經緯度和地址等資訊。

3)實作簽到規則校驗

簽到規則校驗需要比較目前時間和簽到規則,並校驗目前位置是否在簽到規則之內。我們可以在簽到方法中加入規則校驗函數。

isSigned(signs, signDate, longitude, latitude) { 
  return ( 
    signs.hasOwnProperty(signDate) && 
    Array.isArray(signs[signDate]) && 
    signs[signDate].some(sign => { 
      let distance = AMap.GeometryUtil.distance( 
        [longitude, latitude], 
        [sign.longitude, sign.latitude]
      ); 
      return distance <= 500; 
    }) 
  ); 
}
登入後複製

此函數需要傳入簽到規則、簽到日期、目前位置等參數,傳回值為布林類型,表示目前位置是否在簽到規則範圍內。

4)完善簽到方法

簽到方法需要完成簽到規則校驗、顯示簽到狀態和儲存簽到記錄等功能。

signIn() { 
  let signDate = this.getNowFormatDate(); 
  let signs = uni.getStorageSync('signs') || {}; 
  if (this.isSigned(signs, signDate, this.longitude, this.latitude)) { 
    this.signInSuccess = true; 
    uni.showToast({ 
      icon: 'none', 
      title: '您已签到' 
    }); 
  } else { 
    this.signInSuccess = false; 
    uni.showToast({ 
      icon: 'none', 
      title: '您未签到' 
    }); 
  } 
  signs[signDate] = signs[signDate] || []; 
  signs[signDate].push({ 
    longitude: this.longitude, 
    latitude: this.latitude, 
    address: this.address 
  }); 
  uni.setStorageSync('signs', signs); 
}
登入後複製

透過上述步驟,我們已經可以實作一個簡單的定位簽到功能。企業可以根據自己的需求,進一步完善和擴展此功能。

總結

本文介紹如何使用uniapp開發一個定位簽到的行動應用程式。透過整合高德地圖SDK並實現授權及定位,我們可以獲得目前設備的位置資訊。透過保存簽到位置資訊、實現簽到規則校驗和完善簽到方法等步驟,我們已經可以實現一個基礎的定位簽到應用。在實踐過程中,讀者可以根據自己的需求進一步完善和擴展此功能,以實現更好的企業管理。

以上是uniapp實現定位簽到的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!