get-location.uvue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex: 1;">
  4. <!-- #endif -->
  5. <page-head :title="title"></page-head>
  6. <view style="padding: 4px">
  7. <text class="hello-text">
  8. 定位功能默认调用操作系统定位API实现。也支持三方SDK定位\n
  9. 部分老款Android手机因gms问题可能导致无法使用系统定位。\n
  10. Web、Android、iOS平台,gcj国标、逆地理信息等功能需调用腾讯定位。</text>
  11. </view>
  12. <view class="uni-padding-wrap uni-common-mt">
  13. <!-- #ifdef APP-ANDROID || APP-IOS -->
  14. <view class="uni-list-cell-db">定位服务商provider(如系统定位,腾讯定位等)</view>
  15. <view class="uni-list" style="margin-bottom: 20px">
  16. <radio-group @change="radioChangePV">
  17. <radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in providerList" :key="item.id"
  18. :class="index < providerList.length - 1 ? 'uni-list-cell-line' : ''" :value="item.id"
  19. :checked="index === currentProvider">
  20. {{ item.name }}
  21. </radio>
  22. </radio-group>
  23. </view>
  24. <!-- #endif -->
  25. <view class="uni-list-cell-db">定位类型</view>
  26. <view class="uni-list">
  27. <radio-group @change="radioChange">
  28. <radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value"
  29. :class="index < items.length - 1 ? 'uni-list-cell-line' : ''" :value="item.value"
  30. :checked="index === current">
  31. {{ item.name }}
  32. </radio>
  33. </radio-group>
  34. </view>
  35. <view class="uni-list-cell uni-list-cell-pd" style="margin-top: 20px">
  36. <view class="uni-list-cell-db">高度信息</view>
  37. <switch :checked="altitudeSelect" @change="altitudeChange" />
  38. </view>
  39. <view class="uni-list-cell uni-list-cell-pd">
  40. <view class="uni-list-cell-db">开启高精度定位</view>
  41. <switch :checked="isHighAccuracySelect" @change="highAccuracySelectChange" />
  42. </view>
  43. <view class="uni-list-cell uni-list-cell-pd">
  44. <view class="uni-list-cell-db">是否解析地址信息</view>
  45. <switch :checked="geocodeSelect" @change="geocodeChange" />
  46. </view>
  47. <view class="get-location-result">{{ exeRet }}</view>
  48. <view class="uni-btn-v">
  49. <button class="uni-btn" type="default" @tap="getLocationTap">
  50. 获取定位
  51. </button>
  52. </view>
  53. </view>
  54. <!-- #ifdef APP -->
  55. </scroll-view>
  56. <!-- #endif -->
  57. </template>
  58. <script lang="uts">
  59. type GetLocationType = 'wgs84' | 'gcj02'
  60. export type LocationItem = { id : string, name : string, provider ?: UniProvider }
  61. export type ItemType = { value : GetLocationType, name : GetLocationType }
  62. export default {
  63. data() {
  64. return {
  65. title: 'get-location',
  66. altitudeSelect: false,
  67. isHighAccuracySelect: false,
  68. geocodeSelect: false,
  69. exeRet: '',
  70. items: [
  71. {
  72. value: 'wgs84',
  73. name: 'wgs84'
  74. },
  75. {
  76. value: 'gcj02',
  77. name: 'gcj02'
  78. }
  79. ] as ItemType[],
  80. providerList: [] as LocationItem[],
  81. current: 0,
  82. currentProvider: 0,
  83. jest_provider: '',
  84. jest_type: 'wgs84' as GetLocationType,
  85. jest_isAltitude: false,
  86. jest_isGeocode: false,
  87. jest_isHighAccuracy: false,
  88. jest_altitude: -1000,
  89. jest_longitude: 200,
  90. jest_latitude: 100,
  91. jest_address: '',
  92. jest_errCode: 0,
  93. jest_complete: false
  94. }
  95. },
  96. onLoad: function () {
  97. // #ifdef APP
  98. this.getProvider()
  99. // #endif
  100. },
  101. methods: {
  102. getProvider() {
  103. // #ifdef APP
  104. let provider = uni.getProviderSync({
  105. service: "location",
  106. } as GetProviderSyncOptions)
  107. console.log(provider)
  108. provider.providerObjects.forEach((value : UniProvider) => {
  109. var currentProvider = value
  110. // if (value.id == 'system') {
  111. // currentProvider = value as UniLocationSystemProvider
  112. // } else if (value.id == 'tencent') {
  113. // currentProvider = value as UniLocationTencentProvider
  114. // }
  115. this.providerList.push({
  116. name: currentProvider.description,
  117. id: currentProvider.id,
  118. provider: currentProvider
  119. } as LocationItem);
  120. })
  121. this.providerList.forEach((value, index) => {
  122. if (value.id == "system") {
  123. this.currentProvider = index
  124. }
  125. })
  126. // #endif
  127. },
  128. altitudeChange: function (e : UniSwitchChangeEvent) {
  129. this.altitudeSelect = e.detail.value
  130. },
  131. geocodeChange: function (e : UniSwitchChangeEvent) {
  132. this.geocodeSelect = e.detail.value
  133. },
  134. highAccuracySelectChange: function (e : UniSwitchChangeEvent) {
  135. this.isHighAccuracySelect = e.detail.value
  136. },
  137. radioChange(e : UniRadioGroupChangeEvent) {
  138. for (let i = 0; i < this.items.length; i++) {
  139. if (this.items[i].value === e.detail.value) {
  140. this.current = i;
  141. break;
  142. }
  143. }
  144. },
  145. radioChangePV(e : UniRadioGroupChangeEvent) {
  146. for (let i = 0; i < this.providerList.length; i++) {
  147. if (this.providerList[i].id === e.detail.value) {
  148. this.currentProvider = i;
  149. break;
  150. }
  151. }
  152. if (e.detail.value == "system") {
  153. this.current = 0
  154. } else if (e.detail.value == "tencent") {
  155. this.current = 1
  156. }
  157. },
  158. getLocationTap: function () {
  159. // #ifdef APP
  160. if (this.providerList.length == 0) {
  161. uni.showToast({
  162. title: '未获取到provider,请确定基座中包含location功能',
  163. icon: "error"
  164. })
  165. console.log("未获取到provider,请确定基座中包含location功能")
  166. return
  167. }
  168. // #endif
  169. uni.showLoading({
  170. title: '定位中'
  171. })
  172. uni.getLocation(({
  173. // #ifdef APP
  174. provider: this.providerList[this.currentProvider].id,
  175. // #endif
  176. type: this.items[this.current].value,
  177. altitude: this.altitudeSelect,
  178. isHighAccuracy: this.isHighAccuracySelect,
  179. geocode: this.geocodeSelect,
  180. success: (res : any) => {
  181. uni.hideLoading()
  182. this.exeRet = JSON.stringify(res)
  183. },
  184. fail: (res : any) => {
  185. uni.hideLoading()
  186. this.exeRet = JSON.stringify(res)
  187. },
  188. complete: (res : any) => {
  189. uni.hideLoading()
  190. this.exeRet = JSON.stringify(res)
  191. }
  192. }));
  193. },
  194. // 仅用于自动化测试
  195. jestGetLocation() {
  196. this.jest_complete = false
  197. this.jest_errCode = 0
  198. uni.getLocation(({
  199. // #ifdef APP
  200. provider: this.jest_provider,
  201. // #endif
  202. type: this.jest_type,
  203. altitude: this.jest_isAltitude,
  204. isHighAccuracy: this.jest_isHighAccuracy,
  205. geocode: this.jest_isGeocode,
  206. success: (res) => {
  207. if (res.address != null) {
  208. this.jest_address = res.address!
  209. }
  210. this.jest_longitude = res.longitude
  211. this.jest_latitude = res.latitude
  212. this.jest_altitude = res.altitude
  213. this.jest_complete = true
  214. },
  215. fail: (err) => {
  216. this.jest_errCode = err.errCode
  217. this.jest_complete = true
  218. }
  219. }));
  220. }
  221. }
  222. }
  223. </script>
  224. <style>
  225. .get-location-result {
  226. /* #ifdef WEB || MP */
  227. word-break: break-all;
  228. /* #endif */
  229. }
  230. </style>