get-location.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view style="background:#FFFFFF; padding:40rpx;">
  6. <view class="uni-hello-text uni-center">当前位置经纬度</view>
  7. <block v-if="hasLocation === false">
  8. <view class="uni-h2 uni-center uni-common-mt">未获取</view>
  9. </block>
  10. <block v-if="hasLocation === true">
  11. <view class="uni-h2 uni-center uni-common-mt">
  12. <text>E: {{location.longitude[0]}}°{{location.longitude[1]}}′</text>
  13. <text>\nN: {{location.latitude[0]}}°{{location.latitude[1]}}′</text>
  14. </view>
  15. </block>
  16. </view>
  17. <view class="uni-btn-v">
  18. <button type="primary" @tap="getLocation">获取位置</button>
  19. <button @tap="clear">清空</button>
  20. </view>
  21. </view>
  22. <uni-popup :show="type === 'showpopup'" mode="fixed" @hidePopup="togglePopup('')">
  23. <view class="popup-view">
  24. <text class="popup-title">需要用户授权位置权限</text>
  25. <view class="uni-flex popup-buttons">
  26. <button class="uni-flex-item" type="primary" open-type="openSetting" @tap="openSetting">设置</button>
  27. <button class="uni-flex-item" @tap="togglePopup('')">取消</button>
  28. </view>
  29. </view>
  30. </uni-popup>
  31. </view>
  32. </template>
  33. <script>
  34. import * as util from '../../../common/util.js'
  35. var formatLocation = util.formatLocation;
  36. // #ifdef APP-PLUS
  37. import permision from "@/common/permission.js"
  38. // #endif
  39. export default {
  40. data() {
  41. return {
  42. title: 'getLocation',
  43. hasLocation: false,
  44. location: {},
  45. type: ''
  46. }
  47. },
  48. methods: {
  49. togglePopup(type) {
  50. this.type = type;
  51. },
  52. showConfirm() {
  53. this.type = 'showpopup';
  54. },
  55. hideConfirm() {
  56. this.type = '';
  57. },
  58. async getLocation() {
  59. // #ifdef APP-PLUS
  60. let status = await this.checkPermission();
  61. if (status !== 1) {
  62. return;
  63. }
  64. // #endif
  65. // #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ
  66. let status = await this.getSetting();
  67. if (status === 2) {
  68. this.showConfirm();
  69. return;
  70. }
  71. // #endif
  72. this.doGetLocation();
  73. },
  74. doGetLocation() {
  75. uni.getLocation({
  76. success: (res) => {
  77. this.hasLocation = true;
  78. this.location = formatLocation(res.longitude, res.latitude);
  79. },
  80. fail: (err) => {
  81. // #ifdef MP-BAIDU
  82. if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny
  83. this.showConfirm();
  84. }
  85. // #endif
  86. // #ifndef MP-BAIDU
  87. if (err.errMsg.indexOf("auth deny") >= 0) {
  88. uni.showToast({
  89. title: "访问位置被拒绝"
  90. })
  91. } else {
  92. uni.showToast({
  93. title: err.errMsg
  94. })
  95. }
  96. // #endif
  97. }
  98. })
  99. },
  100. getSetting: function() {
  101. return new Promise((resolve, reject) => {
  102. uni.getSetting({
  103. success: (res) => {
  104. if (res.authSetting['scope.userLocation'] === undefined) {
  105. resolve(0);
  106. return;
  107. }
  108. if (res.authSetting['scope.userLocation']) {
  109. resolve(1);
  110. } else {
  111. resolve(2);
  112. }
  113. }
  114. });
  115. });
  116. },
  117. openSetting: function() {
  118. this.hideConfirm();
  119. uni.openSetting({
  120. success: (res) => {
  121. if (res.authSetting && res.authSetting['scope.userLocation']) {
  122. this.doGetLocation();
  123. }
  124. },
  125. fail: (err) => {}
  126. })
  127. },
  128. async checkPermission() {
  129. let status = permision.isIOS ? await permision.requestIOS('location') :
  130. await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');
  131. if (status === null || status === 1) {
  132. status = 1;
  133. } else if (status === 2) {
  134. uni.showModal({
  135. content: "系统定位已关闭",
  136. confirmText: "确定",
  137. showCancel: false,
  138. success: function(res) {
  139. }
  140. })
  141. } else if (status.code) {
  142. uni.showModal({
  143. content: status.message
  144. })
  145. } else {
  146. uni.showModal({
  147. content: "需要定位权限",
  148. confirmText: "设置",
  149. success: function(res) {
  150. if (res.confirm) {
  151. permision.gotoAppSetting();
  152. }
  153. }
  154. })
  155. }
  156. return status;
  157. },
  158. clear: function() {
  159. this.hasLocation = false
  160. }
  161. }
  162. }
  163. </script>
  164. <style>
  165. .popup-view {
  166. width: 500rpx;
  167. }
  168. .popup-title {
  169. display: block;
  170. font-size: 16px;
  171. line-height: 3;
  172. margin-bottom: 10px;
  173. text-align: center;
  174. }
  175. .popup-buttons button {
  176. margin-left: 4px;
  177. margin-right: 4px;
  178. }
  179. </style>