uniapp小程序項目獲取位置經緯度信息
更新時間:2022年11月03日 17:18:49 作者:芳草斜陽_晨光
在實際項目中很多時候我們需要獲取設備的位置信息,去展示給客戶,或者以位置信息為參數,繼續(xù)向服務器獲取一些數據,這篇文章主要介紹了uni-app如何獲取位置信息(經緯度),需要的朋友可以參考下
前言
提示:這里可以添加本文要記錄的大概內容:
在實際項目中很多時候我們需要獲取設備的位置信息,去展示給客戶,或者以位置信息為參數,繼續(xù)向服務器獲取一些數據。接下來以uni-app小程序項目為例來介紹獲取位置信息的思路
提示:以下是本篇文章正文內容,下面案例可供參考
一、相關代碼
- 判斷手機定位是否授權
// 定位授權 getLocation() { let that = this; // 1、判斷手機定位服務【GPS】 是否授權 uni.getSystemInfo({ success(res) { console.log("判斷手機定位服務是否授權:", res); let locationEnabled = res.locationEnabled; //判斷手機定位服務是否開啟 let locationAuthorized = res.locationAuthorized; //判斷定位服務是否允許微信授權 if (locationEnabled == false || locationAuthorized == false) { //手機定位服務(GPS)未授權 uni.showToast({ title: "請打開手機GPS", icon: "none", }); } else { //手機定位服務(GPS)已授權 // 2、判斷微信小程序是否授權位置信息 // 微信小程序已授權位置信息 uni.authorize({ //授權請求窗口 scope: "scope.userLocation", //授權的類型 success: (res) => { that.fnGetlocation(); }, fail: (err) => { err = err["errMsg"]; uni .showModal({ content: "需要授權位置信息", confirmText: "確認授權", }) .then((res) => { console.log(res); if (res[1]["confirm"]) { uni.openSetting({ success: (res) => { if (res.authSetting["scope.userLocation"]) { // 授權成功 uni.showToast({ title: "授權成功", icon: "none", }); that.fnGetlocation(); } else { // 未授權 uni.showToast({ title: "授權失敗,請重新授權", icon: "none", }); uni.showModal({ title: "授權", content: "獲取授權" + authouName + "失敗,是否前往授權設置?", success: function (result) { if (result.confirm) { uni.openSetting(); } }, fail: function () { uni.showToast({ title: "系統(tǒng)錯誤!", icon: "none", }); }, }); } }, }); } if (res[1]["cancel"]) { // 取消授權 uni.showToast({ title: "你拒絕了授權,無法獲得周邊信息", icon: "none", }); } }); }, complete(res) { // console.log('授權彈框', res); if (res.errMsg == "authorize:ok") { that.fnGetlocation(); } else { uni.showModal({ title: "授權", content: "獲取授權" + authouName + "失敗,是否前往授權設置?", success: function (result) { if (result.confirm) { uni.openSetting(); } }, fail: function () { uni.showToast({ title: "系統(tǒng)錯誤!", icon: "none", }); }, }); } }, }); } }, }); },
- 判斷小程序是否授權位置信息 (代碼在上方)
- 定位獲取
// 定位獲取 fnGetlocation() { let that = this; uni.getLocation({ type: "wgs84", //默認為 wgs84 返回 gps 坐標 geocode: "true", isHighAccuracy: "true", accuracy: "best", // 精度值為20m success: function (res) { console.log("定位獲取:", res); let platform = uni.getSystemInfoSync().platform; if (platform == "ios") { //toFixed() 方法可把 Number 四舍五入為指定小數位數的數字。 that.bindList.long = res.longitude.toFixed(6); that.bindList.lat = res.latitude.toFixed(6); } else { that.bindList.long = res.longitude; that.bindList.lat = res.latitude; } that.bindList.longlat = "經度" + that.changeTwoDecimal_f(that.bindList.long) + "/" + "緯度" + that.changeTwoDecimal_f(that.bindList.lat); that.getAreaCode(res.latitude, res.longitude); }, fail(err) { if ( err.errMsg === "getLocation:fail 頻繁調用會增加電量損耗,可考慮使用 wx.onLocationChange 監(jiān)聽地理位置變化" ) { uni.showToast({ title: "請勿頻繁定位", icon: "none", }); } if (err.errMsg === "getLocation:fail auth deny") { // 未授權 uni.showToast({ title: "無法定位,請重新獲取位置信息", icon: "none", }); authDenyCb && authDenyCb(); that.isLocated = false; } if ( err.errMsg === "getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF" ) { uni.showModal({ content: "請開啟手機定位服務", showCancel: false, }); } }, }); },
- 通過經緯度坐標獲取區(qū)域碼
// getAreaCode通過經緯度(wgs84)坐標獲取區(qū)域碼 getAreaCode(latitude, longitude) { this.$refs.uForm.resetFields(); var that = this; that.$u.api .getAreaCode({ latitude: latitude, longitude: longitude, }) .then((res) => { if (res.code == 100000000) { console.log("通過經緯度坐標獲取區(qū)域碼:", res); // console.log(res, 'areaCode'); that.bindList.areaCode = res.data.areaCode; that.bindList.specificAddress = res.data.detailLocation; that.bindList.address = res.data.areaLocation; } else { uni.showToast({ title: res.msg, icon: "none" }); } }) .catch((err) => { this.loadState = "加載失敗err"; console.log("getDevList_err:", err); //-------------------- }); },
二、相關的數據返回
三、效果展示
最后
提示:這里對文章進行總結:
以上就是獲取位置信息的大概步驟思路:
- 判斷手機定位服務是否授權(uni.getSystemInfo)
- 判斷小程序是否授權位置信息(uni.authorize)
- 定位獲?。╱ni.getLocation)
- 通過經緯度坐標獲取區(qū)域碼,這是通過以經緯度為參數獲取后端的數據
到此這篇關于uni-app如何獲取位置信息(經緯度)的文章就介紹到這了,更多相關uni-app獲取位置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于Bootstrap的Java開發(fā)問題匯總(Spring MVC)
這篇文章主要為大家匯總了基于Bootstrap的Java開發(fā)問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01javascript制作sql轉換為stringBuffer的小工具
這篇文章主要介紹了javascript制作sql轉換為stringBuffer的小工具,使用方法很簡單,吧寫好的sql語句只要格式化好之后放進去就可以了,推薦給大家,有需要的小伙伴可以參考下。2015-04-04