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

uniapp 小程序獲取定位流程

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

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

 

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

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

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

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

進(jìn)入定位頁(yè)面就會(huì)彈出,如果沒(méi)有彈出,說(shuō)明小程序的位置服務(wù)不可用,去小程序后臺(tái)查看下

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

否則如果點(diǎn)擊拒絕,就會(huì)彈窗提示打開定位

如果點(diǎn)擊取消,說(shuō)明用戶不需要獲取位置那么就返回上一頁(yè)

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

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

標(biāo)記位置和拖動(dòng)地圖

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

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

data里面配置下地圖標(biāo)記點(diǎn)

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

拖動(dòng)地圖時(shí)獲取地圖的中心點(diǎn)的經(jīng)緯度

regionchange(e) {
        // 更新當(dāng)前搜索周邊的經(jīng)緯度
        if (e.type == 'end') {
          console.log(e, "拖動(dòng)后的經(jīng)緯度");
          // 需要獲取地圖中心的經(jīng)緯度 否則無(wú)法確定唯一經(jīng)緯度
          this.getCenterLanLat()
        }
      },
      
      // 獲取當(dāng)前地圖中心的經(jīng)緯度
      getCenterLanLat() {
        uni.createMapContext("map"this).getCenterLocation({
          type'gcj02',
          success: (res) => {
            console.log("地圖中心點(diǎn)經(jīng)緯度:", res);
            // 把當(dāng)前經(jīng)緯度設(shè)置給標(biāo)記點(diǎn)
            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'',
        // 地圖中心標(biāo)記點(diǎn)
        markers: [{
          id0// 使用 marker點(diǎn)擊事件 需要填寫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('當(dāng)前位置的經(jīng)度:' + res.longitude);
            console.log('當(dāng)前位置的緯度:' + res.latitude);
            this.longitude = res.longitude
            this.latitude = res.latitude
            // 調(diào)用后端接口根據(jù)得到的經(jīng)緯度獲取地址
            console.log(res, "根據(jù)經(jīng)緯度獲取地址");
          },
          // 若用戶點(diǎn)擊拒絕獲取位置則彈出提示
          fail: (err) => {
            uni.showModal({
              content'檢測(cè)到您沒(méi)打開獲取位置功能權(quán)限,是否去設(shè)置打開?',
              confirmText"確認(rèn)",
              cancelText'取消',
              success(res) => {
                if (res.confirm) {
                  uni.openSetting({
                    success(res) => {
                      uni.showToast({
                        title'授權(quán)后請(qǐng)重新打開此頁(yè)面',
                        icon'none'
                      })
                    },
                    fail(err) => {
                      console.log(err)
                    }
                  })
                } else {
                  uni.showToast({
                    title'獲取地理位置授權(quán)失敗',
                    icon'none',
                    success() => {
                      // 返回上一頁(yè)
                      setTimeout(() => {
                        uni.showToast({
                          title"返回上一頁(yè)",
                          icon'none'
                        })
                        // uni.navigateBack({
                        // delta: 1
                        // })
                      }, 500)
                    }
                  })
                }
              }
            })
          },
        })
      },


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

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

<style>

</style>

 效果:


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

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
在线视频亚洲精品 | 亚洲精品在线直播 | 亚洲中文字幕91在线 | 午夜性色福利视频你懂的 | 一本大道道久久九九AV综合 | 日韩精品香蕉999 |