当前位置:   article > 正文

微信小程序获取用户定位(经纬度)信息_微信小程序获取位置信息

微信小程序获取位置信息

1.只获取定位方法:控制台实现

https://blog.csdn.net/weixin_44593720/article/details/1089975822

 2.在wxml页面显示经纬度以及城市位置信息:

微信小程序-获取用户位置(经纬度+所在城市)_微信小程序根据经纬度获取详细地址_Mocode的博客-CSDN博客

3.获取当前定位与指定位置的差值:(方法一可用)

3分钟搞定微信小程序类美团用户商家距离计算 | 微信开放社区

4.坐标拾取器:

点图拾取坐标-地图开放平台|腾讯位置服务

5.计算当前距离到指定距离的直线距离:(首先要实现第二步的配置信息)

①js代码:

  1. Page({
  2. data:{
  3. s:''
  4. },
  5. onLoad: function() {
  6. var _this = this;
  7. _this.findXy() //查询用户与商家的距离
  8. },
  9. findXy() { //获取用户的经纬度
  10. var _this = this
  11. wx.getLocation({
  12. type: 'gcj02',
  13. success(res) {
  14. distance : _this.getDistance(res.latitude, res.longitude, 23.152354,113.341697)
  15. }
  16. })
  17. },
  18. Rad: function(d) { //根据经纬度判断距离
  19. return d * Math.PI / 180.0;
  20. },
  21. getDistance: function(lat1, lng1, lat2, lng2) {
  22. // lat1用户的纬度
  23. // lng1用户的经度
  24. // lat2商家的纬度
  25. // lng2商家的经度
  26. var radLat1 = this.Rad(lat1);
  27. var radLat2 = this.Rad(lat2);
  28. var a = radLat1 - radLat2;
  29. var b = this.Rad(lng1) - this.Rad(lng2);
  30. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  31. s = s * 6378.137;
  32. s = Math.round(s * 10000) / 10000;
  33. s = s.toFixed(2) + '公里' //保留两位小数
  34. // console.log('经纬度计算的距离:' + s)
  35. console.log('您距离学校中心的距离:' + s)
  36. //回调渲染到前端页面
  37. this.setData({
  38. s:s
  39. })
  40. return s
  41. },
  42. })

②wxml代码:

<view bind:tap="getDistance" style="background-color: rgb(222, 221, 135);height: 100px;font-size: 30px;">您当前距离学校中心的距离为:{{s}}</view>

③app.json中:

  1. "permission": {
  2. "scope.userLocation": {
  3. "desc": "你的位置信息将用于小程序位置接口的效果展示"
  4. }
  5. },

6.如果在项目中不显示内容,可以在app.json文件中加入以下代码进行授权:

主要是对“getLocation”进行授权

  1. "requiredPrivateInfos": [
  2. "getLocation",
  3. "onLocationChange",
  4. "startLocationUpdateBackground",
  5. "chooseAddress",
  6. "chooseLocation",
  7. "choosePoi"
  8. ]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/713600
推荐阅读
相关标签
  

闽ICP备14008679号