您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“uniapp小程序如何獲取位置經(jīng)緯度信息”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“uniapp小程序如何獲取位置經(jīng)緯度信息”吧!
判斷手機(jī)定位是否授權(quán)
// 定位授權(quán) getLocation() { let that = this; // 1、判斷手機(jī)定位服務(wù)【GPS】 是否授權(quán) uni.getSystemInfo({ success(res) { console.log("判斷手機(jī)定位服務(wù)是否授權(quán):", res); let locationEnabled = res.locationEnabled; //判斷手機(jī)定位服務(wù)是否開啟 let locationAuthorized = res.locationAuthorized; //判斷定位服務(wù)是否允許微信授權(quán) if (locationEnabled == false || locationAuthorized == false) { //手機(jī)定位服務(wù)(GPS)未授權(quán) uni.showToast({ title: "請打開手機(jī)GPS", icon: "none", }); } else { //手機(jī)定位服務(wù)(GPS)已授權(quán) // 2、判斷微信小程序是否授權(quán)位置信息 // 微信小程序已授權(quán)位置信息 uni.authorize({ //授權(quán)請求窗口 scope: "scope.userLocation", //授權(quán)的類型 success: (res) => { that.fnGetlocation(); }, fail: (err) => { err = err["errMsg"]; uni .showModal({ content: "需要授權(quán)位置信息", confirmText: "確認(rèn)授權(quán)", }) .then((res) => { console.log(res); if (res[1]["confirm"]) { uni.openSetting({ success: (res) => { if (res.authSetting["scope.userLocation"]) { // 授權(quán)成功 uni.showToast({ title: "授權(quán)成功", icon: "none", }); that.fnGetlocation(); } else { // 未授權(quán) uni.showToast({ title: "授權(quán)失敗,請重新授權(quán)", icon: "none", }); uni.showModal({ title: "授權(quán)", content: "獲取授權(quán)" + authouName + "失敗,是否前往授權(quán)設(shè)置?", success: function (result) { if (result.confirm) { uni.openSetting(); } }, fail: function () { uni.showToast({ title: "系統(tǒng)錯誤!", icon: "none", }); }, }); } }, }); } if (res[1]["cancel"]) { // 取消授權(quán) uni.showToast({ title: "你拒絕了授權(quán),無法獲得周邊信息", icon: "none", }); } }); }, complete(res) { // console.log('授權(quán)彈框', res); if (res.errMsg == "authorize:ok") { that.fnGetlocation(); } else { uni.showModal({ title: "授權(quán)", content: "獲取授權(quán)" + authouName + "失敗,是否前往授權(quán)設(shè)置?", success: function (result) { if (result.confirm) { uni.openSetting(); } }, fail: function () { uni.showToast({ title: "系統(tǒng)錯誤!", icon: "none", }); }, }); } }, }); } }, }); },
判斷小程序是否授權(quán)位置信息 (代碼在上方)
定位獲取
// 定位獲取 fnGetlocation() { let that = this; uni.getLocation({ type: "wgs84", //默認(rèn)為 wgs84 返回 gps 坐標(biāo) geocode: "true", isHighAccuracy: "true", accuracy: "best", // 精度值為20m success: function (res) { console.log("定位獲取:", res); let platform = uni.getSystemInfoSync().platform; if (platform == "ios") { //toFixed() 方法可把 Number 四舍五入為指定小數(shù)位數(shù)的數(shù)字。 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 = "經(jīng)度" + 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 頻繁調(diào)用會增加電量損耗,可考慮使用 wx.onLocationChange 監(jiān)聽地理位置變化" ) { uni.showToast({ title: "請勿頻繁定位", icon: "none", }); } if (err.errMsg === "getLocation:fail auth deny") { // 未授權(quán) uni.showToast({ title: "無法定位,請重新獲取位置信息", icon: "none", }); authDenyCb && authDenyCb(); that.isLocated = false; } if ( err.errMsg === "getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF" ) { uni.showModal({ content: "請開啟手機(jī)定位服務(wù)", showCancel: false, }); } }, }); },
通過經(jīng)緯度坐標(biāo)獲取區(qū)域碼
// getAreaCode通過經(jīng)緯度(wgs84)坐標(biāo)獲取區(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("通過經(jīng)緯度坐標(biāo)獲取區(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); //-------------------- }); },
到此,相信大家對“uniapp小程序如何獲取位置經(jīng)緯度信息”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。