微信小程序获取地理位置

这篇博客详细介绍了在微信小程序中如何获取并使用用户位置信息。通过app.json配置权限,onLoad函数触发getLocation方法,成功获取经纬度后存储到本地,并在控制台输出以供检查。在遇到失败情况时,还提供了错误处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先微信公众平台开通权限

app.json

 "permission":{
    "scope.userLocation": {
      "desc": "您的位置信息将用于获取周围门店信息"
    }
  },
  "requiredPrivateInfos": [
		"getLocation"
	],
 "sitemapLocation": "sitemap.json",

index.js

 onLoad: function () {
this.getLocation()
},
 getLocation() {
   
    console.log('pppp')
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        console.log(res)
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        console.log(res) //将获取到的经纬度信息输出到控制台以便检查
     

        that.setData({ //将获取到的经度、纬度数值分别赋值给本地变量
          latitude: res.latitude,
          longitude: res.longitude,
          location:res.latitude+','+res.longitude
        })
        console.log(res.latitude+','+res.longitude)
        wx.setStorageSync('location', res.latitude+','+res.longitude)//arr是要存的数据
        
      },
      fail(err){
        console.log(err);
      }
     })
  },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值