LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

uniapp 小程序獲取定位流程

admin
2024年7月30日 14:17 本文熱度 4307

01. 在uniapp項目的manifest.json中的位置接口,描述文字就是提示文字:

 

02. 在 uniapp 項目源碼視圖的微信小程序代碼中添加如下:

/* 小程序特有相關(guān) */
"mp-weixin" : {
        ...... // 原有的
        // 新增的
        "permission" : {
        "scope.userLocation" : {
          "desc" : "位置測試"
         }
   },
        "requiredPrivateInfos" : [ "getLocation" ]
 },

03. 在需要獲取定位的頁面寫如下方法,然后onload里面調(diào)用此方法

getlocation() {
        uni.getLocation({
          type: 'wgs84',
          success: (res) => {
            console.log('當前位置的經(jīng)度:' + res.longitude);
            console.log('當前位置的緯度:' + res.latitude);
            this.longitude = res.longitude
            this.latitude = res.latitude
            // 調(diào)用后端接口根據(jù)得到的經(jīng)緯度獲取地址
            console.log(res, "根據(jù)經(jīng)緯度獲取地址");
          },
          // 若用戶點擊拒絕獲取位置則彈出提示
          fail: (err) => {
            uni.showModal({
              content: '檢測到您沒打開獲取位置功能權(quán)限,是否去設(shè)置打開?',
              confirmText: "確認",
              cancelText: '取消',
              success: (res) => {
                if (res.confirm) {
                  uni.openSetting({
                    success: (res) => {
                      uni.showToast({
                        title: '授權(quán)后請重新打開此頁面',
                        icon: 'none'
                      })
                    },
                    fail: (err) => {
                      console.log(err)
                    }
                  })
                } else {
                  uni.showToast({
                    title: '獲取地理位置授權(quán)失敗',
                    icon: 'none',
                    success: () => {
                      // 返回上一頁
                      setTimeout(() => {
                        uni.showToast({
                          title: "返回上一頁",
                          icon: 'none'
                        })
                        // uni.navigateBack({
                        //   delta: 1
                        // })
                      }, 500)
                    }
                  })
                }
              }
            })
          },
        })
      },

進入定位頁面就會彈出,如果沒有彈出,說明小程序的位置服務(wù)不可用,去小程序后臺查看下

如果點擊允許按鈕,那么就會獲得當前位置的經(jīng)緯度

否則如果點擊拒絕,就會彈窗提示打開定位

如果點擊取消,說明用戶不需要獲取位置那么就返回上一頁

如果點擊確認就會彈出設(shè)置權(quán)限

這一步在模擬器上管用,切換為允許就會正常獲取到經(jīng)緯度了,但是在真機上允許還是不行,你必須手動打開手機的定位才可以

標記位置和拖動地圖

獲取到定位之后,將定位展示在地圖上,使用自帶的map組件,實現(xiàn)拖動地圖標記出當前地圖的中心點位置,此時要修改下map標簽

<map :latitude="latitude" :longitude="longitude" id="map" ref="map" :markers="markers"
      @regionchange='regionchange' style="width: 100%; height: 1200rpx;"></map>

data里面配置下地圖標記點

markers: [{
  id0// 使用 marker點擊事件 需要填寫id
  latitude0,
  longitude0,
  iconPath'https://bpic.51yuansu.com/pic3/cover/00/77/39/58c6caf1c78e3_610.jpg',
  // 設(shè)置markers的寬高
  width30,
  height40,
}]

拖動地圖時獲取地圖的中心點的經(jīng)緯度

regionchange(e) {
        // 更新當前搜索周邊的經(jīng)緯度
        if (e.type == 'end') {
          console.log(e, "拖動后的經(jīng)緯度");
          // 需要獲取地圖中心的經(jīng)緯度 否則無法確定唯一經(jīng)緯度
          this.getCenterLanLat()
        }
      },
      
      // 獲取當前地圖中心的經(jīng)緯度
      getCenterLanLat() {
        uni.createMapContext("map"this).getCenterLocation({
          type'gcj02',
          success: (res) => {
            console.log("地圖中心點經(jīng)緯度:", res);
            // 把當前經(jīng)緯度設(shè)置給標記點
            this.markers[0].latitude = res.latitude
            this.markers[0].longitude = res.longitude
          },
          fail: (err) => {}
        })
      }, 

  

完整源碼


<template>
  <view>
    <map :latitude="latitude" :longitude="longitude" id="map" ref="map" :markers="markers"
      @regionchange='regionchange' style="width: 100%; height: 1200rpx;">
</map>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        longitude'',
        latitude'',
        // 地圖中心標記點
        markers: [{
          id0// 使用 marker點擊事件 需要填寫id
          latitude: 0,
          longitude0,
          iconPath'https://bpic.51yuansu.com/pic3/cover/00/77/39/58c6caf1c78e3_610.jpg',
          // 設(shè)置markers的寬高
          width: 30,
          height40,
        }]
      }
    },

    onLoad() {
      this.getlocation()
    },

    methods: {
      getlocation() {
        uni.getLocation({
          type'wgs84',
          success(res) => {
            console.log('當前位置的經(jīng)度:' + res.longitude);
            console.log('當前位置的緯度:' + res.latitude);
            this.longitude = res.longitude
            this.latitude = res.latitude
            // 調(diào)用后端接口根據(jù)得到的經(jīng)緯度獲取地址
            console.log(res, "根據(jù)經(jīng)緯度獲取地址");
          },
          // 若用戶點擊拒絕獲取位置則彈出提示
          fail: (err) => {
            uni.showModal({
              content'檢測到您沒打開獲取位置功能權(quán)限,是否去設(shè)置打開?',
              confirmText"確認",
              cancelText'取消',
              success(res) => {
                if (res.confirm) {
                  uni.openSetting({
                    success(res) => {
                      uni.showToast({
                        title'授權(quán)后請重新打開此頁面',
                        icon'none'
                      })
                    },
                    fail(err) => {
                      console.log(err)
                    }
                  })
                } else {
                  uni.showToast({
                    title'獲取地理位置授權(quán)失敗',
                    icon'none',
                    success() => {
                      // 返回上一頁
                      setTimeout(() => {
                        uni.showToast({
                          title"返回上一頁",
                          icon'none'
                        })
                        // uni.navigateBack({
                        // delta: 1
                        // })
                      }, 500)
                    }
                  })
                }
              }
            })
          },
        })
      },


      regionchange(e) {
        // 更新當前搜索周邊的經(jīng)緯度
        if (e.type == 'end') {
          console.log(e, "拖動后的經(jīng)緯度");
          // 需要獲取地圖中心的經(jīng)緯度 否則無法確定唯一經(jīng)緯度
          this.getCenterLanLat()
        }
      },

      // 獲取當前地圖中心的經(jīng)緯度
      getCenterLanLat() {
        uni.createMapContext("map"this).getCenterLocation({
          type'gcj02',
          success(res) => {
            console.log("地圖中心點經(jīng)緯度:", res);
            // 把當前經(jīng)緯度設(shè)置給標記點
            this.markers[0].latitude = res.latitude
            this.markers[0].longitude = res.longitude
          },
          fail(err) => {}
        })
      },
    }
  }
</script>

<style>

</style>

 效果:


該文章在 2024/7/30 17:01:51 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲a人一区二区免费 | 亚洲V国产V日韩V欧美V | 亚洲成a人网站在线看 | 午夜福利福利视频网 | 日本中文字幕在线免费观看一区二区 | 亚洲人成网a在线播放 |