Marker.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <div>
  3. <slot></slot>
  4. </div>
  5. </template>
  6. <script>
  7. import commonMixin from '../base/mixins/common.js'
  8. import bindEvents from '../base/bindEvent.js'
  9. import { createLabel, createIcon, createPoint } from '../base/factory.js'
  10. import { deleteEmptyKey } from '../base/util.js'
  11. export default {
  12. name: 'bm-marker',
  13. mixins: [commonMixin('overlay')],
  14. inject: {
  15. Cluster: {
  16. default: () => ({})
  17. }
  18. },
  19. props: {
  20. position: {},
  21. offset: {},
  22. icon: {},
  23. massClear: {
  24. type: Boolean,
  25. default: true
  26. },
  27. dragging: {
  28. type: Boolean,
  29. default: false
  30. },
  31. clicking: {
  32. type: Boolean,
  33. default: true
  34. },
  35. raiseOnDrag: {
  36. type: Boolean,
  37. default: false
  38. },
  39. draggingCursor: {
  40. type: String
  41. },
  42. rotation: {
  43. type: Number
  44. },
  45. shadow: {
  46. type: Object
  47. },
  48. title: {
  49. type: String
  50. },
  51. label: {
  52. type: Object
  53. },
  54. animation: {
  55. type: String
  56. },
  57. top: {
  58. type: Boolean,
  59. default: false
  60. },
  61. zIndex: {
  62. type: Number,
  63. default: 0
  64. }
  65. },
  66. watch: {
  67. 'position.lng'(val, oldVal) {
  68. const { BMap, originInstance, position, renderByParent, $parent } = this
  69. if (val !== oldVal && val >= -180 && val <= 180) {
  70. originInstance.setPosition(createPoint(BMap, { lng: val, lat: position.lat }))
  71. }
  72. renderByParent && $parent.reload()
  73. },
  74. 'position.lat'(val, oldVal) {
  75. const { BMap, originInstance, position, renderByParent, $parent } = this
  76. if (val !== oldVal && val >= -74 && val <= 74) {
  77. originInstance.setPosition(createPoint(BMap, { lng: position.lng, lat: val }))
  78. }
  79. renderByParent && $parent.reload()
  80. },
  81. 'offset.width'(val, oldVal) {
  82. const { BMap, originInstance } = this
  83. if (val !== oldVal) {
  84. originInstance.setOffset(new BMap.Size(val, this.offset.height))
  85. }
  86. },
  87. 'offset.height'(val, oldVal) {
  88. const { BMap, originInstance } = this
  89. if (val !== oldVal) {
  90. originInstance.setOffset(new BMap.Size(this.offset.width, val))
  91. }
  92. },
  93. icon: {
  94. deep: true,
  95. handler(val) {
  96. const { BMap, originInstance, rotation } = this
  97. originInstance && originInstance.setIcon(createIcon(BMap, val))
  98. rotation && originInstance && originInstance.setRotation(rotation)
  99. }
  100. },
  101. massClear(val) {
  102. val ? this.originInstance.enableMassClear() : this.originInstance.disableMassClear()
  103. },
  104. dragging(val) {
  105. val ? this.originInstance.enableDragging() : this.originInstance.disableDragging()
  106. },
  107. clicking() {
  108. this.reload()
  109. },
  110. raiseOnDrag() {
  111. this.reload()
  112. },
  113. draggingCursor(val) {
  114. this.originInstance.setDraggingCursor(val)
  115. },
  116. rotation(val) {
  117. this.originInstance.setRotation(val)
  118. },
  119. shadow(val) {
  120. this.originInstance.setShadow(val)
  121. },
  122. title(val) {
  123. this.originInstance.setTitle(val)
  124. },
  125. label(val) {
  126. this.reload()
  127. },
  128. animation(val) {
  129. this.originInstance.setAnimation(window[val])
  130. },
  131. top(val) {
  132. this.originInstance.setTop(val)
  133. },
  134. zIndex(val) {
  135. this.originInstance.setZIndex(val)
  136. }
  137. },
  138. methods: {
  139. load() {
  140. const { BMap, map, position, offset, icon, massClear, dragging, clicking, raiseOnDrag, draggingCursor, rotation, shadow, title, label, animation, top, renderByParent, $parent, zIndex } = this
  141. let makerOption = {
  142. offset,
  143. icon: icon && createIcon(BMap, icon),
  144. enableMassClear: massClear,
  145. enableDragging: dragging,
  146. enableClicking: clicking,
  147. raiseOnDrag,
  148. draggingCursor,
  149. rotation,
  150. shadow,
  151. title
  152. };
  153. deleteEmptyKey(makerOption);
  154. const overlay = new BMap.Marker(new BMap.Point(position.lng, position.lat), makerOption)
  155. this.originInstance = overlay
  156. label && overlay && overlay.setLabel(createLabel(BMap, label))
  157. overlay.setTop(top)
  158. overlay.setZIndex(zIndex)
  159. bindEvents.call(this, overlay)
  160. if (renderByParent) {
  161. $parent.reload()
  162. } else {
  163. map.addOverlay(overlay)
  164. }
  165. overlay.setAnimation(window[animation])
  166. this.Cluster?.addMaker?.(this);
  167. }
  168. },
  169. beforeUnmount() {
  170. this.Cluster?.removeMaker?.(this);
  171. }
  172. }
  173. </script>
  174. <style lang="scss">
  175. .BMap_Marker img {
  176. max-width: none;
  177. }
  178. </style>