You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
2.9 KiB

  1. <template>
  2. <view
  3. class="addressMap"
  4. v-if="position.latitude && addressList.length">
  5. <map
  6. style="width: 100%;height: 500rpx;border-radius: 20rpx;"
  7. :layer-style='5'
  8. :show-location='true'
  9. :latitude="positionMap.latitude"
  10. :longitude="positionMap.longitude"
  11. :markers="addressMarkers"
  12. :scale="scale"
  13. @markertap="markertap"
  14. id="mapId"
  15. @callouttap="callouttap">
  16. </map>
  17. </view>
  18. </template>
  19. <script>
  20. import positionMixin from '@/mixins/position';
  21. export default {
  22. mixins: [positionMixin],
  23. name:"addressMap",
  24. props : {
  25. addressList : {
  26. default : []
  27. },
  28. },
  29. computed : {
  30. addressMarkers(){
  31. if (!this.addressList ||
  32. this.addressList.length == 0 ||
  33. !this.position ||
  34. !this.position.latitude ||
  35. !this.position.longitude) {
  36. return []
  37. }
  38. this.addressList.forEach((item, index) => {
  39. let distance = this.calculateDistance(
  40. this.position.latitude,
  41. this.position.longitude,
  42. item.latitude,
  43. item.longitude,
  44. )
  45. item.width = 20 //图标icon 宽度
  46. item.height = 28 //图标icon 高度
  47. item.distance = distance
  48. item.iconPath = ''
  49. })
  50. this.addressList.sort((a,b) => {
  51. return a.distance - b.distance
  52. })
  53. // 计算最大距离并设置缩放比例
  54. if (this.addressList.length > 1) {
  55. let maxDistance = 0
  56. for (let i = 0; i < this.addressList.length; i++) {
  57. for (let j = i + 1; j < this.addressList.length; j++) {
  58. const distance = this.calculateDistance(
  59. this.addressList[i].latitude,
  60. this.addressList[i].longitude,
  61. this.addressList[j].latitude,
  62. this.addressList[j].longitude
  63. )
  64. maxDistance = Math.max(maxDistance, distance)
  65. }
  66. }
  67. // 根据最大距离设置缩放比例
  68. // 距离越大,缩放比例越小
  69. if (maxDistance > 100) { //
  70. this.scale = 5
  71. }else if (maxDistance > 50) { //
  72. this.scale = 8
  73. }else if (maxDistance > 10) { // 10公里以上
  74. this.scale = 10
  75. } else if (maxDistance > 5) { // 5-10公里
  76. this.scale = 11
  77. } else if (maxDistance > 2) { // 2-5公里
  78. this.scale = 13
  79. } else { // 2公里以内
  80. this.scale = 15
  81. }
  82. }
  83. return this.addressList
  84. },
  85. positionMap(){
  86. if(this.addressMarkers.length == 0){
  87. return {
  88. latitude : 0,
  89. longitude : 0,
  90. }
  91. }
  92. return {
  93. latitude : this.addressMarkers[0].latitude,
  94. longitude : this.addressMarkers[0].longitude,
  95. }
  96. }
  97. },
  98. data() {
  99. return {
  100. scale : 15,
  101. };
  102. },
  103. methods : {
  104. markertap(e){
  105. console.log(e);
  106. this.addressList.forEach(item => {
  107. if(item.id == e.detail.markerId){
  108. this.$emit('clickAddress', item)
  109. }
  110. })
  111. },
  112. callouttap(e){},
  113. },
  114. }
  115. </script>
  116. <style scoped lang="scss">
  117. .addressMap{
  118. padding-bottom: 30rpx;
  119. }
  120. </style>