• 微信小程序一键获取位置


    需求 有个表单需要一键获取对应位置 并显示出来效果如下:

    点击一键获取获取对应位置 显示在  picker 默认选中

    前端  代码如下:

    1. <view class="box_7 {{ showChange==1? 'change-style':'' }}">
    2. <view class="box_11">
    3. <view class="text-wrapper_6 {{ showChange==1? 'change-style-postiona':'' }}">
    4. <text lines="1" class="text_29">考试地址text>
    5. <text lines="1" class="text_30" />
    6. <text lines="1" class="text_31">*text>
    7. view>
    8. <picker class="region-select" mode="region" bindchange="bindRegionChange" value="{{region}}" custom-item="{{customItem}}">
    9. <view class="picker">
    10. {{region[0]}} {{region[1]}} {{region[2]}}
    11. view>
    12. picker>
    13. <button bindtap="getUserLocation" class="get-position {{ showChange==1? 'change-style-postion':''}}">一键获取button>
    14. view>
    15. view>

    一定注意:小程序中要配置,不然获取位置不生效。

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

    小程序js如下:注意 获取到 经纬度之后 请求腾讯 api key 要进行申请 ,申请很快

    申请地址 腾讯位置服务 - 立足生态,连接未来

    1. getUserLocation: function () {
    2. let that = this;
    3. wx.getSetting({
    4. success: (res) => {
    5. if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true)
    6. {//如果没有授权就提示需要授权
    7. wx.showModal({
    8. title: '请求授权当前位置',
    9. content: '需要获取您的地理位置,请确认授权',
    10. success: function (res) {
    11. if (res.cancel) {
    12. wx.showToast({
    13. title: '拒绝授权',
    14. icon: 'none',
    15. duration: 1000
    16. })
    17. } else if (res.confirm) {
    18. wx.openSetting({
    19. success: function (dataAu) {
    20. if (dataAu.authSetting["scope.userLocation"] == true) {
    21. wx.showToast({
    22. title: '授权成功',
    23. icon: 'success',
    24. duration: 1000
    25. })
    26. //再次授权,调用wx.getLocation的API
    27. that.getLocations();
    28. } else {
    29. wx.showToast({
    30. title: '授权失败',
    31. icon: 'none',
    32. duration: 1000
    33. })
    34. }
    35. }
    36. })
    37. }
    38. }
    39. })
    40. } else if (res.authSetting['scope.userLocation'] == undefined) {
    41. that.getLocations();
    42. }
    43. else {
    44. that.getLocations();
    45. }
    46. }
    47. })
    48. },
    49. //获取经纬度
    50. getLocations()
    51. {
    52. let that = this;
    53. wx.getLocation({
    54. type: 'wgs84',
    55. success (res) {
    56. const latitude = res.latitude
    57. const longitude = res.longitude
    58. wx.request({
    59. url: 'https://apis.map.qq.com/ws/geocoder/v1/',
    60. data: {
    61. location: `${latitude},${longitude}`,
    62. key: that.data.key,
    63. get_poi: 0
    64. },
    65. success(res) {
    66. if(res.statusCode==200){
    67. const addressComponent = res.data.result.address_component;
    68. const province = addressComponent.province;
    69. const city = addressComponent.city;
    70. const district = addressComponent.district;
    71. var showChange =0;
    72. if((province+city+district).length>=12){
    73. showChange =1;
    74. }
    75. var result = [province, city, district];
    76. that.setData({
    77. region: result,
    78. showChange: showChange,
    79. addressSelect:1,
    80. })
    81. }else{
    82. wx.showToast({
    83. title: '定位获取失败,请手动选择~',
    84. icon: 'none'
    85. })
    86. }
    87. }
    88. })
    89. }
    90. })
    91. },

  • 相关阅读:
    Python图像处理库打开图片默认的维度顺序
    15-JavaSE基础巩固练习:多态、接口、抽象类的综合练习
    2022-08-06 第四小组 修身课 学习笔记(every day)
    交换机端口映射
    2022 【SPDK原理最新视频讲解】
    如何获取springboot中所有的bean
    AC8015笔记
    基于C#实现的小型动物识别推理系统
    upp(企业统一流程平台)阶段性汇报20220909
    zookeeper可视化界面zkui
  • 原文地址:https://blog.csdn.net/qq_25861247/article/details/133911911