choose-location.uvue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view class="uni-column uni-container align-center">
  6. <text class="uni-hello-text">位置信息</text>
  7. <text v-if="!hasLocation" class="uni-title-text uni-common-mt">未选择位置</text>
  8. <view v-if="hasLocation" class="align-center">
  9. <text class="uni-common-mt">{{locationName}}</text>
  10. <text class="uni-common-mt">{{locationAddress}}</text>
  11. <view class="uni-common-mt" v-if="location.latitude.length>1">
  12. <text>E: {{location.longitude[0]}}°{{location.longitude[1]}}′</text>
  13. <text>\nN: {{location.latitude[0]}}°{{location.latitude[1]}}′</text>
  14. </view>
  15. </view>
  16. </view>
  17. <view class="uni-btn-v">
  18. <text class="tips">注意:\n1. Web和App需要正确配置地图服务商的Key并且保证Key的权限和余额足够,才能正常选择位置\n2. 若没有关联uniCloud空间,则只能全屏地图选点,不能根据POI选择位置\n3. payload参数会原样透传给uni-map-co,可用于用户鉴权</text>
  19. <boolean-data :defaultValue="false" title="是否指定位置为天安门" @change="changeLocationBoolean"></boolean-data>
  20. <boolean-data :defaultValue="false" title="是否携带keyword参数" @change="changeKeywordBoolean"></boolean-data>
  21. <!-- #ifndef MP -->
  22. <boolean-data :defaultValue="false" title="是否携带payload参数" @change="changePayloadBoolean"></boolean-data>
  23. <!-- #endif -->
  24. <button class="uni-btn" type="primary" @tap="chooseLocation">选择位置</button>
  25. <button class="uni-btn" @tap="clear">清空</button>
  26. <!-- #ifdef APP-IOS -->
  27. <button class="uni-btn" type="primary" @tap="chooseLocationByPlugin">通过 uts 插件调用 chooseLocation</button>
  28. <!-- #endif -->
  29. </view>
  30. </view>
  31. </view>
  32. </template>
  33. <script lang="uts">
  34. import {
  35. state,
  36. setLifeCycleNum
  37. } from '@/store/index.uts'
  38. type Location = {
  39. latitude: string[]
  40. longitude: string[]
  41. }
  42. export default {
  43. data() {
  44. return {
  45. title: 'chooseLocation',
  46. hasLocation: false,
  47. location: {
  48. latitude: [],
  49. longitude: []
  50. } as Location,
  51. locationName: '',
  52. locationAddress: '',
  53. dialogPagesNum: -1,
  54. hoverLocation: false,
  55. hoverKeyword: false,
  56. hoverPayload: false
  57. }
  58. },
  59. onShow() {
  60. console.log("Page Show");
  61. // 自动化测试
  62. setLifeCycleNum(state.lifeCycleNum + 1)
  63. },
  64. onHide() {
  65. console.log("Page Hide");
  66. // 自动化测试
  67. setLifeCycleNum(state.lifeCycleNum - 1)
  68. },
  69. methods: {
  70. chooseLocation: function () {
  71. let chooseLocationOptions = {
  72. success: (res) => {
  73. console.log('chooseLocation success', res)
  74. this.hasLocation = true
  75. this.location = this.formatLocation(res.longitude, res.latitude)
  76. this.locationName = res.name
  77. this.locationAddress = res.address
  78. }
  79. } as ChooseLocationOptions
  80. if (this.hoverLocation) {
  81. chooseLocationOptions.latitude = 39.908823
  82. chooseLocationOptions.longitude = 116.39747
  83. }
  84. if (this.hoverKeyword) {
  85. chooseLocationOptions.keyword = '公园'
  86. }
  87. // #ifndef MP
  88. if (this.hoverPayload) {
  89. chooseLocationOptions.payload = {
  90. token: 'xxx'
  91. }
  92. }
  93. // #endif
  94. uni.chooseLocation(chooseLocationOptions)
  95. // 自动化测试
  96. setTimeout(() => {
  97. this.test()
  98. }, 500)
  99. },
  100. formatLocation: function(longitude:number, latitude:number):Location {
  101. const longitudeArr = longitude.toString().split('.')
  102. const latitudeArr = latitude.toString().split('.')
  103. if(longitudeArr.length>1){
  104. longitudeArr[1] = longitudeArr[1].substring(0,2)
  105. }
  106. if(latitudeArr.length>1){
  107. latitudeArr[1] = latitudeArr[1].substring(0,2)
  108. }
  109. return {
  110. longitude: longitudeArr,
  111. latitude: latitudeArr
  112. }
  113. },
  114. clear: function () {
  115. this.hasLocation = false
  116. },
  117. changeLocationBoolean(checked : boolean) {
  118. this.hoverLocation = checked
  119. },
  120. changeKeywordBoolean(checked : boolean) {
  121. this.hoverKeyword = checked
  122. },
  123. changePayloadBoolean(checked : boolean) {
  124. this.hoverPayload = checked
  125. },
  126. // #ifdef APP-IOS
  127. chooseLocationByPlugin(){
  128. uni.chooseLocationPlugin()
  129. },
  130. // #endif
  131. // 自动化测试
  132. test() {
  133. const pages = getCurrentPages()
  134. const page = pages[pages.length - 1]
  135. // #ifdef APP || WEB
  136. const dialogPages = page.getDialogPages()
  137. this.dialogPagesNum = dialogPages.length
  138. // #endif
  139. },
  140. // 自动化测试
  141. setLifeCycleNum(value : number) {
  142. setLifeCycleNum(value)
  143. },
  144. // 自动化测试
  145. getLifeCycleNum() : number {
  146. return state.lifeCycleNum
  147. },
  148. }
  149. }
  150. </script>
  151. <style>
  152. .page-body-info {
  153. padding-bottom: 0;
  154. height: 220px;
  155. }
  156. .align-center{
  157. align-items: center;
  158. }
  159. .tips {
  160. font-size: 12px;
  161. margin: 15px 0;
  162. opacity: .8;
  163. }
  164. </style>