kongdeqiang
2023-07-02 fa99d6f0de7d6e7d601542eefa5b040f38c69f98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<script>
import commonMixin from '../base/mixins/common.js'
import bindEvents from '../base/bindEvent.js'
import {createPoint, createSize} from '../base/factory.js'
 
export default {
  name: 'bm-label',
  render () {},
  mixins: [commonMixin('overlay')],
  props: {
    content: {
      type: String
    },
    title: {
      type: String
    },
    offset: {},
    position: {},
    labelStyle: {},
    zIndex: {
      type: Number,
      default: 0
    },
    massClear: {
      type: Boolean,
      default: true
    }
  },
  watch: {
    content (val) {
      this.originInstance.setContent(val)
    },
    title (val) {
      this.originInstance.setTitle(val)
    },
    'offset.width' (val, oldVal) {
      const {BMap} = this
      if (val.toString() !== oldVal.toString()) {
        this.originInstance.setOffset(createSize(BMap, {width: val, height: this.offset.height}))
      }
    },
    'offset.height' (val, oldVal) {
      const {BMap} = this
      if (val.toString() !== oldVal.toString()) {
        this.originInstance.setOffset(createSize(BMap, {
          width: this.offset.width,
          height: val
        }))
      }
    },
    'position.lng' (val, oldVal) {
      const {BMap} = this
      const lng = val
      if (val.toString() !== oldVal.toString() && lng >= -180 && lng <= 180) {
        this.originInstance.setCenter(createPoint(BMap, {lng, lat: this.center.lat}))
      }
    },
    'position.lat' (val, oldVal) {
      const {BMap} = this
      const lat = val
      if (val.toString() !== oldVal.toString() && lat >= -74 && lat <= 74) {
        this.originInstance.setCenter(createPoint(BMap, {lng: this.center.lng, lat}))
      }
    },
    labelStyle: {
      handler (val) {
        this.originInstance.setStyle(val)
      },
      deep: true
    },
    zIndex (val) {
      this.originInstance.setZIndex(val)
    },
    massClear (val) {
      val ? this.originInstance.enableMassClear() : this.originInstance.disableMassClear()
    }
  },
  methods: {
    load () {
      const {BMap, map, content, title, offset, position, labelStyle, zIndex, massClear, $parent} = this
      const overlay = new BMap.Label(content, {
        offset: createSize(BMap, offset),
        position: createPoint(BMap, position),
        enableMassClear: massClear
      })
      this.originInstance = overlay
      try {
        $parent.originInstance.setLabel(overlay)
      } catch (e) {
        map.addOverlay(overlay)
      }
      title && overlay.setTitle(title)
      labelStyle && overlay.setStyle(labelStyle)
      zIndex && overlay.setZIndex(zIndex)
      bindEvents.call(this, overlay)
    }
  }
}
</script>