map.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <view class="map-page">
  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. let pages = getCurrentPages(); //获取所有页面栈实例列表
  33. let nowPage = pages[ pages.length - 1]; //当前页页面实例
  34. let prevPage = pages[ pages.length - 2 ]; //上一页页面实例
  35. prevPage.$vm.model.receiveAdress= res.address; // 修改上一页data里面的content参数值
  36. uni.navigateBack()
  37. // that.submit()
  38. },
  39. fail: () => {
  40. uni.showToast({
  41. title: '微信登录授权失败',
  42. icon: 'none'
  43. });
  44. }
  45. });
  46. },
  47. methods: {
  48. //获取经纬度
  49. getLocation() {
  50. let that = this;
  51. uni.getLocation({
  52. type: 'gcj02',
  53. success: (res) => {
  54. let {
  55. latitude,
  56. longitude
  57. } = res;
  58. that.latitude = latitude;
  59. that.longitude = longitude;
  60. // that.submit()
  61. },
  62. })
  63. },
  64. //地图视野发生变化
  65. regionchange(e) {
  66. if (e.type === 'end') {
  67. let {
  68. latitude,
  69. longitude
  70. } = e.detail.centerLocation;
  71. this.setCovers(latitude, longitude);
  72. } else if (e.type == 'regionchange') {
  73. let self = this
  74. this.mapContext = uni.createMapContext("map", this);
  75. this.mapContext.getCenterLocation({
  76. type: 'gcj02',
  77. success: (res) => {
  78. let {
  79. latitude,
  80. longitude
  81. } = res;
  82. this.setCovers(latitude, longitude);
  83. },
  84. fail: (err) => {
  85. // console.log('获取当前地图中心的经纬度2', err);
  86. }
  87. })
  88. }
  89. },
  90. //设置点位
  91. setCovers(latitude, longitude) {
  92. let location = {
  93. id: "0",
  94. latitude: Number(latitude),
  95. longitude: Number(longitude),
  96. width: uni.upx2px(50),
  97. height: uni.upx2px(55),
  98. iconPath: './../../static/location.png'
  99. }
  100. this.covers = [location]
  101. },
  102. //提交
  103. async submit() {
  104. let that = this
  105. uni.showLoading({
  106. title: '提交中'
  107. });
  108. // that.myAmapFun.getRegeo({
  109. // location: `${this.longitude},${this.latitude}`,
  110. // success: function(res) {
  111. // uni.hideLoading();
  112. // if(res.length > 0) {
  113. // uni.$emit('changeAddress', res[0]);
  114. // uni.navigateBack({
  115. // delta: 1
  116. // });
  117. // } else {
  118. // this.res= JSON.stringify(res)
  119. // uni.$u.toast('地址解析失败');
  120. // }
  121. // },
  122. // fail: function(res) {
  123. // uni.hideLoading();
  124. // uni.$u.toast('地址解析失败');
  125. // }
  126. // })
  127. let postMap = {
  128. longitude: this.longitude,
  129. latitude: this.latitude,
  130. }
  131. const {code, data, msg} = await this.$u.api.gdAnalysisAddress(postMap)
  132. if(code === 200) {
  133. if(data.province) {
  134. uni.$emit('changeAddress', data);
  135. uni.navigateBack({
  136. delta: 1
  137. });
  138. } else {
  139. uni.$u.toast('地址解析失败');
  140. }
  141. } else {
  142. uni.hideLoading();
  143. uni.$u.toast('地址解析失败');
  144. }
  145. },
  146. }
  147. }
  148. </script>
  149. <style lang="scss">
  150. page {
  151. height: 100%;
  152. }
  153. .map-page {
  154. height: 100%;
  155. .map {
  156. width: 100%;
  157. height: 100%;
  158. }
  159. .btn-box {
  160. position: fixed;
  161. left: 0;
  162. bottom: 0;
  163. right: 0;
  164. padding: 15rpx;
  165. padding-bottom: calc(constant(safe-area-inset-bottom) + 15rpx);
  166. padding-bottom: calc(env(safe-area-inset-bottom) + 15rpx);
  167. .btn {
  168. height: 88rpx;
  169. line-height: 88rpx;
  170. text-align: center;
  171. font-size: 32rpx;
  172. background: #0ACBCE;
  173. border-radius: 49rpx;
  174. color: #fff;
  175. }
  176. }
  177. }
  178. </style>