get-location.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 uniPopup from '@/components/uni-popup/uni-popup.vue'
  35. var util = require('../../../common/util.js');
  36. var formatLocation = util.formatLocation;
  37. // #ifdef APP-PLUS
  38. import permision from "@/common/permission.js"
  39. // #endif
  40. export default {
  41. components: {
  42. uniPopup
  43. },
  44. data() {
  45. return {
  46. title: 'getLocation',
  47. hasLocation: false,
  48. location: {},
  49. type: ''
  50. }
  51. },
  52. methods: {
  53. togglePopup(type) {
  54. this.type = type;
  55. },
  56. showConfirm() {
  57. this.type = 'showpopup';
  58. },
  59. hideConfirm() {
  60. this.type = '';
  61. },
  62. async getLocation() {
  63. // #ifdef APP-PLUS
  64. let status = await this.checkPermission();
  65. if (status !== 1) {
  66. return;
  67. }
  68. // #endif
  69. // #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ
  70. let status = await this.getSetting();
  71. if (status === 2) {
  72. this.showConfirm();
  73. return;
  74. }
  75. // #endif
  76. this.doGetLocation();
  77. },
  78. doGetLocation() {
  79. uni.getLocation({
  80. success: (res) => {
  81. this.hasLocation = true;
  82. this.location = formatLocation(res.longitude, res.latitude);
  83. },
  84. fail: (err) => {
  85. // #ifdef MP-BAIDU
  86. if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny
  87. this.showConfirm();
  88. }
  89. // #endif
  90. // #ifndef MP-BAIDU
  91. if (err.errMsg.indexOf("auth deny") >= 0) {
  92. uni.showToast({
  93. title: "访问位置被拒绝"
  94. })
  95. } else {
  96. uni.showToast({
  97. title: err.errMsg
  98. })
  99. }
  100. // #endif
  101. }
  102. })
  103. },
  104. getSetting: function() {
  105. return new Promise((resolve, reject) => {
  106. uni.getSetting({
  107. success: (res) => {
  108. if (res.authSetting['scope.userLocation'] === undefined) {
  109. resolve(0);
  110. return;
  111. }
  112. if (res.authSetting['scope.userLocation']) {
  113. resolve(1);
  114. } else {
  115. resolve(2);
  116. }
  117. }
  118. });
  119. });
  120. },
  121. openSetting: function() {
  122. this.hideConfirm();
  123. uni.openSetting({
  124. success: (res) => {
  125. if (res.authSetting && res.authSetting['scope.userLocation']) {
  126. this.doGetLocation();
  127. }
  128. },
  129. fail: (err) => {}
  130. })
  131. },
  132. async checkPermission() {
  133. let status = permision.isIOS ? await permision.requestIOS('location') :
  134. await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');
  135. if (status === null || status === 1) {
  136. status = 1;
  137. } else if (status === 2) {
  138. uni.showModal({
  139. content: "系统定位已关闭",
  140. confirmText: "确定",
  141. showCancel: false,
  142. success: function(res) {
  143. }
  144. })
  145. } else if (status.code) {
  146. uni.showModal({
  147. content: status.message
  148. })
  149. } else {
  150. uni.showModal({
  151. content: "需要定位权限",
  152. confirmText: "设置",
  153. success: function(res) {
  154. if (res.confirm) {
  155. permision.gotoAppSetting();
  156. }
  157. }
  158. })
  159. }
  160. return status;
  161. },
  162. clear: function() {
  163. this.hasLocation = false
  164. }
  165. }
  166. }
  167. </script>
  168. <style>
  169. .popup-view {
  170. width: 500rpx;
  171. }
  172. .popup-title {
  173. display: block;
  174. font-size: 16px;
  175. line-height: 3;
  176. margin-bottom: 10px;
  177. text-align: center;
  178. }
  179. .popup-buttons button {
  180. margin-left: 4px;
  181. margin-right: 4px;
  182. }
  183. </style>