91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

uniapp小程序如何獲取位置經緯度信息

發布時間:2023-01-10 17:59:18 來源:億速云 閱讀:183 作者:iii 欄目:開發技術

本篇內容主要講解“uniapp小程序如何獲取位置經緯度信息”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“uniapp小程序如何獲取位置經緯度信息”吧!

一、相關代碼

  • 判斷手機定位是否授權

// 定位授權
    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: "系統錯誤!",
                                  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: "系統錯誤!",
                        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 監聽地理位置變化"
          ) {
            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,
            });
          }
        },
      });
    },
  • 通過經緯度坐標獲取區域碼

// getAreaCode通過經緯度(wgs84)坐標獲取區域碼
    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("通過經緯度坐標獲取區域碼:", 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小程序如何獲取位置經緯度信息

到此,相信大家對“uniapp小程序如何獲取位置經緯度信息”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

宁晋县| 蓝山县| 辽阳市| 牟定县| 衡水市| 乳源| 宾川县| 宝丰县| 永泰县| 万荣县| 大安市| 友谊县| 霍城县| 青龙| 通化县| 太白县| 沈丘县| 冀州市| 新宁县| 永宁县| 泽普县| 沽源县| 武夷山市| 邯郸县| 东乡族自治县| 嘉善县| 铜陵市| 正镶白旗| 武夷山市| 宁津县| 潼南县| 华安县| 徐州市| 奎屯市| 古蔺县| 吴江市| 建瓯市| 大安市| 榆社县| 沂南县| 贵阳市|