'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import { MapType, getMapInfo, IS_AMAP } from '../../../helpers/location'
import getRealPath from 'uni-platform/helpers/get-real-path'
 
export default {
  props: {
    id: {
      type: [Number, String],
      default: ''
    },
    latitude: {
      type: [Number, String],
      require: true
    },
    longitude: {
      type: [Number, String],
      require: true
    },
    title: {
      type: String,
      default: ''
    },
    iconPath: {
      type: String,
      require: true
    },
    rotate: {
      type: [Number, String],
      default: 0
    },
    alpha: {
      type: [Number, String],
      default: 1
    },
    width: {
      type: [Number, String],
      default: ''
    },
    height: {
      type: [Number, String],
      default: ''
    },
    callout: {
      type: Object,
      default: null
    },
    label: {
      type: Object,
      default: null
    },
    anchor: {
      type: Object,
      default: null
    },
    clusterId: {
      type: [Number, String],
      default: ''
    },
    customCallout: {
      type: Object,
      default: null
    },
    ariaLabel: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      idString: String(isNaN(Number(this.id)) ? '' : this.id)
    }
  },
  mounted () {
    const $parent = this.$parent
    $parent.mapReady(() => {
      this._maps = $parent._maps
      this._map = $parent._map
      this.addMarker(this.$props)
      Object.keys(this.$props).forEach(key => {
        this.$watch(key, () => {
          this.updateMarker(this.$props)
        })
      })
    })
  },
  beforeDestroy () {
    this.removeMarker()
  },
  methods: {
    addMarker (props) {
      const maps = this._maps
      const map = this._map
      const marker = this._marker = new maps.Marker({
        map,
        flat: true,
        autoRotation: false
      })
      this.$parent._markers[this.idString] = marker
      this.updateMarker(props)
      if (IS_AMAP) {
        // 不通过 addListener 方式绑定事件,为了规避高德地图覆盖物点击触发map点击问题
        marker.dom.addEventListener('click', e => {
          this.handleAMapMarkerClick(e, marker)
        })
        // 地图缩放会触发touchend,故判断 touchstart 和 touchend touches[0] 是否相同确定是否点击
        let touchStartTouch = {}
        marker.dom.addEventListener('touchstart', e => {
          touchStartTouch = e.touches[0]
        })
        marker.dom.addEventListener('touchend', e => {
          const touchEndTouch = e.changedTouches[0]
          if (touchStartTouch.clientX === touchEndTouch.clientX && touchStartTouch.clientY === touchEndTouch.clientY) {
            this.handleAMapMarkerClick(e, marker)
          }
        })
      } else {
        maps.event.addListener(marker, 'click', (e) => {
          const callout = marker.callout
          if (callout && !callout.alwaysVisible) {
            callout.set('visible', !callout.visible)
            if (callout.visible) {
              const div = callout.div
              const parent = div.parentNode
              parent.removeChild(div)
              parent.appendChild(div)
            }
          }
 
          const event = e.event || e.domEvent
 
          if (this.idString) {
            const { latitude, longitude } = this.getMarkerLatitudeLongitude(e)
            this.$parent.$trigger('markertap', event, {
              markerId: Number(this.idString),
              latitude,
              longitude
            })
          }
          event.stopPropagation()
        })
      }
    },
    updateMarker (option) {
      const map = this._map
      const maps = this._maps
      const marker = this._marker
      const title = option.title
      const position = IS_AMAP ? new maps.LngLat(option.longitude, option.latitude) : new maps.LatLng(option.latitude, option.longitude)
      const img = new Image()
      let imgHeight = 0
      img.onload = () => {
        const anchor = option.anchor || {}
        let icon
        let w
        let h
        const x = typeof anchor.x === 'number' ? anchor.x : 0.5
        const y = typeof anchor.y === 'number' ? anchor.y : 1
        if (option.iconPath && (option.width || option.height)) {
          w = option.width || (img.width / img.height) * option.height
          h = option.height || (img.height / img.width) * option.width
        } else {
          w = img.width / 2
          h = img.height / 2
        }
        imgHeight = h
        const top = h - (h - y * h)
        if ('MarkerImage' in maps) {
          // 腾讯 & google
          icon = new maps.MarkerImage(
            img.src,
            null,
            null,
            new maps.Point(x * w, y * h),
            new maps.Size(w, h)
          )
        } else if ('Icon' in maps) {
          // 高德
          icon = new maps.Icon({
            image: img.src,
            size: new maps.Size(w, h),
            imageSize: new maps.Size(w, h),
            imageOffset: new maps.Pixel(x * w, y * h)
          })
        } else {
          icon = {
            url: img.src,
            anchor: new maps.Point(x, y),
            size: new maps.Size(w, h)
          }
        }
        marker.setPosition(position)
        marker.setIcon(icon)
        if ('setRotation' in marker) {
          marker.setRotation(option.rotate || 0)
        }
        const labelOpt = option.label || {}
        if ('label' in marker) {
          marker.label.setMap(null)
          delete marker.label
        }
        let label
        if (labelOpt.content) {
          const labelStyle = {
            borderColor: labelOpt.borderColor,
            borderWidth: (Number(labelOpt.borderWidth) || 0) + 'px',
            padding: (Number(labelOpt.padding) || 0) + 'px',
            borderRadius: (Number(labelOpt.borderRadius) || 0) + 'px',
            backgroundColor: labelOpt.bgColor,
            color: labelOpt.color,
            fontSize: (labelOpt.fontSize || 14) + 'px',
            lineHeight: (labelOpt.fontSize || 14) + 'px',
            marginLeft: (Number(labelOpt.anchorX || labelOpt.x) || 0) + 'px',
            marginTop: (Number(labelOpt.anchorY || labelOpt.y) || 0) + 'px'
          }
          if ('Label' in maps) {
            // 腾讯
            label = new maps.Label({
              position,
              map: map,
              clickable: false,
              content: labelOpt.content,
              style: labelStyle
            })
            marker.label = label
          } else if ('setLabel' in marker) {
            if (IS_AMAP) {
              const content =
              `<div style="
                margin-left:${labelStyle.marginLeft};
                margin-top:${labelStyle.marginTop};
                padding:${labelStyle.padding};
                background-color:${labelStyle.backgroundColor};
                border-radius:${labelStyle.borderRadius};
                line-height:${labelStyle.lineHeight};
                color:${labelStyle.color};
                font-size:${labelStyle.fontSize};
 
                ">
                ${labelOpt.content}
              <div>`
              marker.setLabel({
                content,
                direction: 'bottom-right'
              })
            } else {
              // google
              const className = this.updateMarkerLabelStyle(this.idString, labelStyle)
              marker.setLabel({
                text: labelOpt.content,
                color: labelStyle.color,
                fontSize: labelStyle.fontSize,
                className
              })
            }
          }
        }
        const calloutOpt = option.callout || {}
        let callout = marker.callout
        let calloutStyle
        if (calloutOpt.content || title) {
          if (IS_AMAP && calloutOpt.content) {
            calloutOpt.content = calloutOpt.content.replaceAll('\n', '<br/>')
          }
          const boxShadow = '0px 0px 3px 1px rgba(0,0,0,0.5)'
          let offsetY = -imgHeight / 2
          if (option.width || option.height) {
            offsetY += 14 - imgHeight / 2
          }
          calloutStyle = calloutOpt.content
            ? {
              position,
              map,
              top,
              // handle AMap callout offset
              offsetY,
              content: calloutOpt.content,
              color: calloutOpt.color,
              fontSize: calloutOpt.fontSize,
              borderRadius: calloutOpt.borderRadius,
              bgColor: calloutOpt.bgColor,
              padding: calloutOpt.padding,
              boxShadow: calloutOpt.boxShadow || boxShadow,
              display: calloutOpt.display
            }
            : {
              position,
              map,
              top,
              offsetY,
              content: title,
              boxShadow: boxShadow
            }
          if (callout) {
            callout.setOption(calloutStyle)
          } else {
            if (IS_AMAP) {
              const callback = ($event, self) => {
                if (self.idString) {
                  self.$parent.$trigger('callouttap', $event, {
                    markerId: Number(self.idString)
                  })
                }
              }
              callout = marker.callout = new maps.Callout(calloutStyle, callback, this)
            } else {
              callout = marker.callout = new maps.Callout(calloutStyle)
              callout.div.onclick = ($event) => {
                if (this.idString) {
                  this.$parent.$trigger('callouttap', $event, {
                    markerId: Number(this.idString)
                  })
                }
                $event.stopPropagation()
                $event.preventDefault()
              }
              // The mobile terminal prevent google map callout click trigger map click
              if (getMapInfo().type === MapType.GOOGLE) {
                callout.div.onpointerdown = (e) => { e.stopPropagation() }
                callout.div.ontouchstart = (e) => { e.stopPropagation() }
              }
            }
          }
        } else {
          if (callout) {
            this.removeMarkerCallout(marker.callout)
            delete marker.callout
          }
        }
      }
      if (option.iconPath) {
        img.src = getRealPath(option.iconPath)
      } else {
        console.error('Marker.iconPath is required.')
      }
    },
    handleAMapMarkerClick (e, marker) {
      const callout = marker.callout
      if (callout && !callout.alwaysVisible) {
        callout.visible = !callout.visible
        if (callout.visible) {
          marker.callout.createAMapText()
        } else {
          marker.callout.removeAMapText()
        }
      }
      if (this.idString) {
        this.$parent.$trigger('markertap', e, {
          markerId: Number(this.idString),
          latitude: marker._position.lat,
          longitude: marker._position.lng
        })
      }
      e.stopPropagation()
    },
    updateMarkerLabelStyle (id, style) {
      const className = 'uni-map-marker-label-' + id
      let styleEl = document.getElementById(className)
      if (!styleEl) {
        styleEl = document.createElement('style')
        styleEl.id = className
        document.head.appendChild(styleEl)
        this.$once('hook:destroyed', () => {
          styleEl.remove()
        })
      }
      const newStyle = Object.assign({}, style, {
        position: 'absolute',
        top: '70px',
        borderStyle: 'solid'
      })
      const div = document.createElement('div')
      Object.keys(newStyle).forEach(key => {
        div.style[key] = newStyle[key] || ''
      })
      styleEl.innerText = `.${className}{${div.getAttribute('style')}}`
      return className
    },
    getMarkerLatitudeLongitude (e) {
      const mapInfo = getMapInfo()
      let latitude
      let longitude
 
      if (IS_AMAP) {
        latitude = e.lnglat.lat
        longitude = e.lnglat.lng
      } else if (mapInfo.type === MapType.QQ) {
        latitude = e.latLng.lat
        longitude = e.latLng.lng
      } else if (mapInfo.type === MapType.GOOGLE) {
        latitude = e.latLng.lat()
        longitude = e.latLng.lng()
      }
      return { latitude, longitude }
    },
    removeMarker () {
      const marker = this._marker
      if (marker) {
        if (marker.label && 'setMap' in marker.label) {
          marker.label.setMap(null)
        }
        if (marker.callout) {
          this.removeMarkerCallout(marker.callout)
        }
        marker.setMap(null)
      }
      delete this.$parent._markers[this.idString]
      this._marker = null
    },
    removeMarkerCallout (callout) {
      if (IS_AMAP) {
        callout.removeAMapText()
      } else {
        callout.setMap(null)
      }
    }
  },
  render () {
    return null
  }
}