map.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <view class="map-page" v-if="false">
  3. <map id="map" class="map" :latitude="latitude" :longitude="longitude" :markers="covers"
  4. @regionchange="regionchange"></map>
  5. <cover-view class="btn-box">
  6. <cover-view class="btn" @click="submit">确认并保存</cover-view>
  7. </cover-view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. res: {},
  15. latitude: 30.659462,
  16. longitude: 104.065735,
  17. covers: [], //存放标记点数组
  18. }
  19. },
  20. onLoad() {
  21. // this.getLocation();
  22. // this.setCovers(this.latitude, this.longitude)
  23. let that = this
  24. uni.chooseLocation({
  25. success: function (res) {
  26. console.log('位置名称:' + res.name);
  27. console.log('详细地址:' + res.address);
  28. console.log('纬度:' + res.latitude);
  29. console.log('经度:' + res.longitude);
  30. that.latitude = res.latitude
  31. that.longitude = res.longitude
  32. that.submit()
  33. },
  34. fail: () => {
  35. uni.showToast({
  36. title: '微信登录授权失败',
  37. icon: 'none'
  38. });
  39. }
  40. });
  41. },
  42. methods: {
  43. //获取经纬度
  44. getLocation() {
  45. let that = this;
  46. uni.getLocation({
  47. type: 'gcj02',
  48. success: (res) => {
  49. let {
  50. latitude,
  51. longitude
  52. } = res;
  53. that.latitude = latitude;
  54. that.longitude = longitude;
  55. // that.submit()
  56. },
  57. })
  58. },
  59. //地图视野发生变化
  60. regionchange(e) {
  61. if (e.type === 'end') {
  62. let {
  63. latitude,
  64. longitude
  65. } = e.detail.centerLocation;
  66. this.setCovers(latitude, longitude);
  67. } else if (e.type == 'regionchange') {
  68. let self = this
  69. this.mapContext = uni.createMapContext("map", this);
  70. this.mapContext.getCenterLocation({
  71. type: 'gcj02',
  72. success: (res) => {
  73. let {
  74. latitude,
  75. longitude
  76. } = res;
  77. this.setCovers(latitude, longitude);
  78. },
  79. fail: (err) => {
  80. // console.log('获取当前地图中心的经纬度2', err);
  81. }
  82. })
  83. }
  84. },
  85. //设置点位
  86. setCovers(latitude, longitude) {
  87. let location = {
  88. id: "0",
  89. latitude: Number(latitude),
  90. longitude: Number(longitude),
  91. width: uni.upx2px(50),
  92. height: uni.upx2px(55),
  93. iconPath: './../../static/location.png'
  94. }
  95. this.covers = [location]
  96. },
  97. //提交
  98. async submit() {
  99. let that = this
  100. uni.showLoading({
  101. title: '提交中'
  102. });
  103. // that.myAmapFun.getRegeo({
  104. // location: `${this.longitude},${this.latitude}`,
  105. // success: function(res) {
  106. // uni.hideLoading();
  107. // if(res.length > 0) {
  108. // uni.$emit('changeAddress', res[0]);
  109. // uni.navigateBack({
  110. // delta: 1
  111. // });
  112. // } else {
  113. // this.res= JSON.stringify(res)
  114. // uni.$u.toast('地址解析失败');
  115. // }
  116. // },
  117. // fail: function(res) {
  118. // uni.hideLoading();
  119. // uni.$u.toast('地址解析失败');
  120. // }
  121. // })
  122. let postMap = {
  123. longitude: this.longitude,
  124. latitude: this.latitude,
  125. }
  126. const {code, data, msg} = await this.$u.api.gdAnalysisAddress(postMap)
  127. if(code === 200) {
  128. if(data.province) {
  129. uni.$emit('changeAddress', data);
  130. uni.navigateBack({
  131. delta: 1
  132. });
  133. } else {
  134. uni.$u.toast('地址解析失败');
  135. }
  136. } else {
  137. uni.hideLoading();
  138. uni.$u.toast('地址解析失败');
  139. }
  140. },
  141. }
  142. }
  143. </script>
  144. <style lang="scss">
  145. page {
  146. height: 100%;
  147. }
  148. .map-page {
  149. height: 100%;
  150. .map {
  151. width: 100%;
  152. height: 100%;
  153. }
  154. .btn-box {
  155. position: fixed;
  156. left: 0;
  157. bottom: 0;
  158. right: 0;
  159. padding: 15rpx;
  160. padding-bottom: calc(constant(safe-area-inset-bottom) + 15rpx);
  161. padding-bottom: calc(env(safe-area-inset-bottom) + 15rpx);
  162. .btn {
  163. height: 88rpx;
  164. line-height: 88rpx;
  165. text-align: center;
  166. font-size: 32rpx;
  167. background: #0ACBCE;
  168. border-radius: 49rpx;
  169. color: #fff;
  170. }
  171. }
  172. }
  173. </style>