LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

uniapp 小程序获取定位流程

admin
2024年7月30日 14:17 本文热度 998

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

 

02. 在 uniapp 项目源码视图的微信小程序代码中添加如下:

/* 小程序特有相关 */
"mp-weixin" : {
        ...... // 原有的
        // 新增的
        "permission" : {
        "scope.userLocation" : {
          "desc" : "位置测试"
         }
   },
        "requiredPrivateInfos" : [ "getLocation" ]
 },

03. 在需要获取定位的页面写如下方法,然后onload里面调用此方法

getlocation() {
        uni.getLocation({
          type: 'wgs84',
          success: (res) => {
            console.log('当前位置的经度:' + res.longitude);
            console.log('当前位置的纬度:' + res.latitude);
            this.longitude = res.longitude
            this.latitude = res.latitude
            // 调用后端接口根据得到的经纬度获取地址
            console.log(res, "根据经纬度获取地址");
          },
          // 若用户点击拒绝获取位置则弹出提示
          fail: (err) => {
            uni.showModal({
              content: '检测到您没打开获取位置功能权限,是否去设置打开?',
              confirmText: "确认",
              cancelText: '取消',
              success: (res) => {
                if (res.confirm) {
                  uni.openSetting({
                    success: (res) => {
                      uni.showToast({
                        title: '授权后请重新打开此页面',
                        icon: 'none'
                      })
                    },
                    fail: (err) => {
                      console.log(err)
                    }
                  })
                } else {
                  uni.showToast({
                    title: '获取地理位置授权失败',
                    icon: 'none',
                    success: () => {
                      // 返回上一页
                      setTimeout(() => {
                        uni.showToast({
                          title: "返回上一页",
                          icon: 'none'
                        })
                        // uni.navigateBack({
                        //   delta: 1
                        // })
                      }, 500)
                    }
                  })
                }
              }
            })
          },
        })
      },

进入定位页面就会弹出,如果没有弹出,说明小程序的位置服务不可用,去小程序后台查看下

如果点击允许按钮,那么就会获得当前位置的经纬度

否则如果点击拒绝,就会弹窗提示打开定位

如果点击取消,说明用户不需要获取位置那么就返回上一页

如果点击确认就会弹出设置权限

这一步在模拟器上管用,切换为允许就会正常获取到经纬度了,但是在真机上允许还是不行,你必须手动打开手机的定位才可以

标记位置和拖动地图

获取到定位之后,将定位展示在地图上,使用自带的map组件,实现拖动地图标记出当前地图的中心点位置,此时要修改下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',
  // 设置markers的宽高
  width30,
  height40,
}]

拖动地图时获取地图的中心点的经纬度

regionchange(e) {
        // 更新当前搜索周边的经纬度
        if (e.type == 'end') {
          console.log(e, "拖动后的经纬度");
          // 需要获取地图中心的经纬度 否则无法确定唯一经纬度
          this.getCenterLanLat()
        }
      },
      
      // 获取当前地图中心的经纬度
      getCenterLanLat() {
        uni.createMapContext("map"this).getCenterLocation({
          type'gcj02',
          success: (res) => {
            console.log("地图中心点经纬度:", res);
            // 把当前经纬度设置给标记点
            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',
          // 设置markers的宽高
          width: 30,
          height40,
        }]
      }
    },

    onLoad() {
      this.getlocation()
    },

    methods: {
      getlocation() {
        uni.getLocation({
          type'wgs84',
          success(res) => {
            console.log('当前位置的经度:' + res.longitude);
            console.log('当前位置的纬度:' + res.latitude);
            this.longitude = res.longitude
            this.latitude = res.latitude
            // 调用后端接口根据得到的经纬度获取地址
            console.log(res, "根据经纬度获取地址");
          },
          // 若用户点击拒绝获取位置则弹出提示
          fail: (err) => {
            uni.showModal({
              content'检测到您没打开获取位置功能权限,是否去设置打开?',
              confirmText"确认",
              cancelText'取消',
              success(res) => {
                if (res.confirm) {
                  uni.openSetting({
                    success(res) => {
                      uni.showToast({
                        title'授权后请重新打开此页面',
                        icon'none'
                      })
                    },
                    fail(err) => {
                      console.log(err)
                    }
                  })
                } else {
                  uni.showToast({
                    title'获取地理位置授权失败',
                    icon'none',
                    success() => {
                      // 返回上一页
                      setTimeout(() => {
                        uni.showToast({
                          title"返回上一页",
                          icon'none'
                        })
                        // uni.navigateBack({
                        // delta: 1
                        // })
                      }, 500)
                    }
                  })
                }
              }
            })
          },
        })
      },


      regionchange(e) {
        // 更新当前搜索周边的经纬度
        if (e.type == 'end') {
          console.log(e, "拖动后的经纬度");
          // 需要获取地图中心的经纬度 否则无法确定唯一经纬度
          this.getCenterLanLat()
        }
      },

      // 获取当前地图中心的经纬度
      getCenterLanLat() {
        uni.createMapContext("map"this).getCenterLocation({
          type'gcj02',
          success(res) => {
            console.log("地图中心点经纬度:", res);
            // 把当前经纬度设置给标记点
            this.markers[0].latitude = res.latitude
            this.markers[0].longitude = res.longitude
          },
          fail(err) => {}
        })
      },
    }
  }
</script>

<style>

</style>

 效果:


该文章在 2024/7/30 17:01:51 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved