mapComponent.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <view class="box">
  3. <map
  4. id="map"
  5. @markertap="markertap"
  6. @callouttap="callouttap"
  7. :enable-overlooking="true"
  8. enable-3D="true"
  9. :scale="scale"
  10. :style="{ width: '100%', height: height + 'rpx' }"
  11. :markers="markers"
  12. :latitude="latitudeAndLongitude.latitude"
  13. :longitude="latitudeAndLongitude.longitude"
  14. :show-location="latitudeAndLongitude.anchorPoint"
  15. >
  16. <cover-view slot="callout">
  17. <cover-view
  18. v-for="item in markers"
  19. :key="item.id"
  20. :marker-id="item.id"
  21. class="custom_callout"
  22. >
  23. <cover-view class="custom_callout_text">{{ item.title }}</cover-view>
  24. </cover-view>
  25. </cover-view>
  26. </map>
  27. </view>
  28. <!-- :show-location="true"定位点 -->
  29. </template>
  30. <script>
  31. export default {
  32. name: 'mapComponent',
  33. props: {
  34. height: {
  35. type: [Number, String],
  36. default: 360
  37. },
  38. latitudeAndLongitude: {
  39. type: Object,
  40. default: {}
  41. },
  42. introduceShow: {
  43. type: Boolean,
  44. default: ''
  45. },
  46. markers: {
  47. type: Array,
  48. default: []
  49. }
  50. },
  51. data() {
  52. return {
  53. latitude: 24.531,
  54. longitude: 118.1918,
  55. scale: 12
  56. };
  57. },
  58. mounted() {
  59. // console.log(this.markers, '子组件地图');
  60. },
  61. methods: {
  62. getMarkerId(event) {
  63. if (event && event.detail && event.detail.markerId !== undefined) {
  64. return event.detail.markerId;
  65. }
  66. if (event && event.markerId !== undefined) {
  67. return event.markerId;
  68. }
  69. return undefined;
  70. },
  71. handleMarkerTap(markerId) {
  72. if (markerId === undefined) {
  73. return;
  74. }
  75. if (this.introduceShow == false) {
  76. this.$emit('subComponent', true, markerId);
  77. } else {
  78. uni.navigateTo({
  79. url: `/index_fenbao/GuanLi/XiangQing?id=${markerId}`
  80. });
  81. }
  82. },
  83. // 点击标记点时触发
  84. markertap(e) {
  85. this.handleMarkerTap(this.getMarkerId(e));
  86. },
  87. callouttap(e) {
  88. this.handleMarkerTap(this.getMarkerId(e));
  89. },
  90. // 获取位置
  91. vicinityBtn() {
  92. const that = this;
  93. uni.getLocation({
  94. type: 'gcj02 ',
  95. geocode: true,
  96. success: function (res) {
  97. // console.log(res, 'resres');
  98. // console.log(that.latitudeAndLongitude, 'this.latitudeAndLongitude');
  99. that.latitudeAndLongitude.longitude = res.longitude;
  100. that.latitudeAndLongitude.latitude = res.latitude;
  101. that.latitudeAndLongitude.anchorPoint = true;
  102. that.$common.successToShow('定位成功');
  103. that.regionTab();
  104. }
  105. });
  106. },
  107. // 定位点
  108. regionTab() {
  109. uni.createMapContext('map', this).moveToLocation({
  110. longitude: this.latitudeAndLongitude.longitude,
  111. latitude: this.latitudeAndLongitude.latitude
  112. });
  113. }
  114. }
  115. };
  116. </script>
  117. <style>
  118. .box {
  119. width: 100%;
  120. }
  121. .custom_callout {
  122. display: flex;
  123. align-items: center;
  124. justify-content: center;
  125. }
  126. .custom_callout_text {
  127. padding: 8rpx 20rpx;
  128. border-radius: 999rpx;
  129. background-color: #e2a0a4;
  130. color: #ffffff;
  131. font-size: 24rpx;
  132. line-height: 1.2;
  133. white-space: nowrap;
  134. text-align: center;
  135. }
  136. .map_box {
  137. width: 625rpx;
  138. height: 320rpx;
  139. margin: auto;
  140. margin-top: 40rpx;
  141. overflow: hidden;
  142. }
  143. </style>