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.

124 lines
2.8 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 > 10) { // 10公里以上
  70. this.scale = 12
  71. } else if (maxDistance > 5) { // 5-10公里
  72. this.scale = 13
  73. } else if (maxDistance > 2) { // 2-5公里
  74. this.scale = 14
  75. } else { // 2公里以内
  76. this.scale = 15
  77. }
  78. }
  79. return this.addressList
  80. },
  81. positionMap(){
  82. if(this.addressMarkers.length == 0){
  83. return {
  84. latitude : 0,
  85. longitude : 0,
  86. }
  87. }
  88. return {
  89. latitude : this.addressMarkers[0].latitude,
  90. longitude : this.addressMarkers[0].longitude,
  91. }
  92. }
  93. },
  94. data() {
  95. return {
  96. scale : 15,
  97. };
  98. },
  99. methods : {
  100. markertap(e){
  101. console.log(e);
  102. this.addressList.forEach(item => {
  103. if(item.id == e.detail.markerId){
  104. this.$emit('clickAddress', item)
  105. }
  106. })
  107. },
  108. callouttap(e){},
  109. },
  110. }
  111. </script>
  112. <style scoped lang="scss">
  113. .addressMap{
  114. padding-bottom: 30rpx;
  115. }
  116. </style>