index.vue 35.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
<template>
  <uni-map
    :id="id"
    class="uni-map" />
</template>

<script>
import {
  subscriber
} from 'uni-mixins'
import {
  supportsPassive
} from 'uni-shared'
var maps
/**
 * 自定义气泡类
 */
var Callout = function (option) {
  this.option = option = option || {}
  var map = option.map
  this.position = option.position
  this.index = 1
  this.visible = this.alwaysVisible = option.display === 'ALWAYS'
  this.init()
  if (map) {
    this.setMap(map)
  }
}
/**
 * 自定义tap事件监听
 */
function onTap (dom, callback) {
  var startTime = 0
  var delayTime = 200
  var x
  var y
  dom.addEventListener('touchstart', function ($event) {
    startTime = Date.now()
    var touch = $event.targetTouches[0]
    x = touch.screenX
    y = touch.screenY
  }, supportsPassive ? { passive: true } : false)
  dom.addEventListener('touchend', function ($event) {
    var touch = $event.changedTouches[0]
    if (Math.abs(touch.screenX - x) > 20 || Math.abs(touch.screenY - y) > 20 || Date.now() - startTime > delayTime) {
      return
    }
    callback($event)
  }, supportsPassive ? { passive: true } : false)
}
export default {
  name: 'Map',
  mixins: [subscriber],
  props: {
    id: {
      type: String,
      default: ''
    },
    longitude: {
      type: [String, Number],
      default: 116.397720
    },
    latitude: {
      type: [String, Number],
      default: 39.903230
    },
    scale: {
      type: [String, Number],
      default: 16
    },
    markers: {
      type: Array,
      default () {
        return []
      }
    },
    covers: {
      type: Array,
      default () {
        return []
      }
    },
    polyline: {
      type: Array,
      default () {
        return []
      }
    },
    circles: {
      type: Array,
      default () {
        return []
      }
    },
    controls: {
      type: Array,
      default () {
        return []
      }
    },
    includePoints: {
      type: Array,
      default () {
        return []
      }
    },
    showLocation: {
      type: [Boolean, String],
      default: false
    }
  },
  data () {
    return {
      /**
       * 地图中心点
       */
      center: {
        latitude: 39.903230,
        longitude: 116.397720
      },
      /**
       * 地图初始化完毕
       */
      isMapReady: false,
      /**
       * 视野初始化完毕
       */
      isBoundsReady: false,
      markersSync: [],
      polylineSync: [],
      circlesSync: [],
      controlsSync: [],
      /**
       * 地图原始对象
       */
      map: null,
      mapLocation: null,
      mapLocationPosition: null
    }
  },
  watch: {
    latitude (val) {
      if (val && this.longitude) {
        this.center.latitude = Number(val)
      }
    },
    longitude (val) {
      if (val && this.latitude) {
        this.center.longitude = Number(val)
      }
    },
    center: {
      handler (val) {
        if (this.map) {
          this.mapReady(() => {
            this.map.setCenter(new maps.LatLng(val.latitude, val.longitude))
          })
        }
      },
      deep: true
    },
    scale (val) {
      this.mapReady(() => {
        this.map.setZoom(Number(val))
      })
    },
    markers (val, old) {
      this.mapReady(() => {
        var add = []
        var has = []
        var changed = []
        var changedOption = []
        var remove = []
        val.forEach(option => {
          if (!('id' in option)) {
            add.push(option)
          } else {
            var isOld = false
            for (var index = 0; index < old.length; index++) {
              var element = old[index]
              if (!('id' in element)) {
                old.splice(index--, 1)
                continue
              }
              if (element.id !== option.id) {
                continue
              }
              isOld = true
              has.push(element.id)
              if (JSON.stringify(element) !== JSON.stringify(option)) {
                changed.push(element.id)
                changedOption.push(option)
              }
              old.splice(index--, 1)
            }
            if (!isOld) {
              add.push(option)
            }
          }
        })
        var markers = this.markersSync
        markers.forEach(marker => {
          var id = marker.id
          var index
          if (has.indexOf(id) >= 0) {
            if ((index = changed.indexOf(id)) >= 0) {
              this.changeMarker(marker, changedOption[index])
            }
          } else {
            remove.push(marker)
          }
        })
        this.removeMarkers(remove)
        this.createMarkers(add)
      })
    },
    polyline (val) {
      this.mapReady(() => {
        this.createPolyline()
      })
    },
    circles () {
      this.mapReady(() => {
        this.createCircles()
      })
    },
    controls () {
      this.mapReady(() => {
        this.createControls()
      })
    },
    includePoints () {
      this.mapReady(() => {
        this.fitBounds(this.includePoints)
      })
    },
    showLocation (val) {
      this.mapReady(() => {
        this[val ? 'createLocation' : 'removeLocation']()
      })
    }
  },
  created () {
    var latitude = this.latitude
    var longitude = this.longitude
    if (latitude && longitude) {
      this.center.latitude = latitude
      this.center.longitude = longitude
    }
  },
  mounted () {
    this.loadMap(() => {
      this.createClass()
      this.init()
    })
  },
  beforeDestroy () {
    this.removeMarkers(this.markersSync)
    this.removePolyline()
    this.removeCircles()
    this.removeControls()
    this.removeLocation()
    this.map = null
  },
  methods: {
    _handleSubscribe ({
      type,
      data = {}
    }) {
      function callback (res, err) {
        res = res || {}
        res.errMsg = `${type}:${err ? 'fail' + err : 'ok'}`
        var cb = err ? data.fail : data.success
        if (typeof cb === 'function') {
          cb(res)
        }
        if (typeof data.complete === 'function') {
          data.complete(res)
        }
      }

      switch (type) {
        case 'getCenterLocation':
          this.mapReady(() => {
            var latitude
            var longitude
            var center = this.map.getCenter()
            latitude = center.getLat()
            longitude = center.getLng()

            callback({
              latitude: latitude,
              longitude: longitude
            })
          })
          break
        case 'moveToLocation':
          var locationPosition = this.mapLocationPosition
          if (locationPosition) {
            this.map.setCenter(locationPosition)
          }
          break
        case 'translateMarker':
          // 此处和小程序一致,不会同步数据到当前子组件
          this.mapReady(() => {
            try {
              var marker = this.getMarker(data.markerId)
              var destination = data.destination
              var duration = data.duration
              var autoRotate = !!data.autoRotate
              var rotate = Number(data.rotate) ? data.rotate : 0
              var rotation = marker.getRotation()
              var a = marker.getPosition()
              var b = new maps.LatLng(destination.latitude, destination.longitude)
              // 距离-千米
              var distance = maps.geometry.spherical.computeDistanceBetween(a, b) / 1000
              // 时间-小时
              var time = ((typeof duration === 'number') ? duration : 1000) / (1000 * 60 * 60)
              // 速度-千米/小时
              var speed = distance / time
              var movingEvent = maps.event.addListener(marker, 'moving', e => {
                var latLng = e.latLng
                var label = marker.label
                if (label) {
                  label.setPosition(latLng)
                }
                var callout = marker.callout
                if (callout) {
                  callout.setPosition(latLng)
                }
              })
              var event = maps.event.addListener(marker, 'moveend', e => {
                event.remove()
                movingEvent.remove()
                marker.lastPosition = a
                marker.setPosition(b)
                var label = marker.label
                if (label) {
                  label.setPosition(b)
                }
                var callout = marker.callout
                if (callout) {
                  callout.setPosition(b)
                }
                var cb = data.animationEnd
                if (typeof cb === 'function') {
                  cb()
                }
              })
              var lastRtate = 0
              if (autoRotate) {
                if (marker.lastPosition) {
                  lastRtate = maps.geometry.spherical.computeHeading(marker.lastPosition, a)
                }
                rotate = maps.geometry.spherical.computeHeading(a, b) - lastRtate
              }
              marker.setRotation(rotation + rotate)
              marker.moveTo(b, speed)
            } catch (error) {
              callback(null, error)
            }
          })
          break
        case 'includePoints':
          // 此处和小程序一致,不会同步数据到当前子组件
          this.fitBounds(data.points)
          break
        case 'getRegion':
          this.boundsReady(() => {
            var latLngBounds = this.map.getBounds()
            var southwest = latLngBounds.getSouthWest()
            var northeast = latLngBounds.getNorthEast()
            callback({
              southwest: {
                latitude: southwest.getLat(),
                longitude: southwest.getLng()
              },
              northeast: {
                latitude: northeast.getLat(),
                longitude: northeast.getLng()
              }
            })
          })
          break
        case 'getScale':
          this.mapReady(() => {
            callback({
              scale: Number(this.scale)
            })
          })
          break
      }
    },
    init () {
      var center = new maps.LatLng(this.center.latitude, this.center.longitude)
      var map = this.map = new maps.Map(this.$el, {
        center,
        zoom: Number(this.scale),
        scrollwheel: false,
        disableDoubleClickZoom: true,
        mapTypeControl: false,
        zoomControl: false,
        scaleControl: false,
        minZoom: 5,
        maxZoom: 18,
        draggable: true
      })
      var boundsChangedEvent = maps.event.addListener(map, 'bounds_changed', e => {
        boundsChangedEvent.remove()
        this.isBoundsReady = true
        this.$emit('boundsready')
      })
      maps.event.addListener(map, 'click', () => {
        this.$trigger('tap', {}, {})
      })
      maps.event.addListener(map, 'dragstart', () => {
        this.$trigger('regionchange', {}, {
          type: 'begin'
        })
      })
      maps.event.addListener(map, 'dragend', () => {
        this.$trigger('regionchange', {}, {
          type: 'end'
        })
      })
      maps.event.addListener(map, 'zoom_changed', () => {
        this.$emit('update:scale', map.getZoom())
      })
      maps.event.addListener(map, 'center_changed', () => {
        var latitude
        var longitude
        var center = map.getCenter()
        latitude = center.getLat()
        longitude = center.getLng()
        this.$emit('update:latitude', latitude)
        this.$emit('update:longitude', longitude)
      })
      if (this.markers && Array.isArray(this.markers) && this.markers.length) {
        this.createMarkers(this.markers)
      }
      if (this.polyline && Array.isArray(this.polyline) && this.polyline.length) {
        this.createPolyline()
      }
      if (this.circles && Array.isArray(this.circles) && this.circles.length) {
        this.createCircles()
      }
      if (this.controls && Array.isArray(this.controls) && this.controls.length) {
        this.createControls()
      }
      if (this.showLocation) {
        this.createLocation()
      }
      if (this.includePoints && Array.isArray(this.includePoints) && this.includePoints.length) {
        this.fitBounds(this.includePoints, () => {
          // 为和小程序一致,重置中心点
          map.setCenter(center)
        })
      }
      // 创建mapContext对象
      // 更改mapReady状态
      this.isMapReady = true
      this.$emit('mapready')
    },
    createMarkers (markerOptions) {
      var map = this.map
      var markers = this.markersSync
      // 创建新标注
      markerOptions.forEach(option => {
        var marker = new maps.Marker({
          map,
          flat: true,
          autoRotation: false
        })
        marker.id = option.id
        this.changeMarker(marker, option)
        // 监听标注事件
        var mouseup
        maps.event.addListener(marker, 'mouseup', e => {
          mouseup = Date.now()
        })
        maps.event.addListener(marker, 'click', e => {
          // 通过过滤暂时解决click事件重复触发的bug
          if (Date.now() - mouseup < 20) {
            return
          }
          var callout = marker.callout
          if (callout) {
            var div = callout.div
            var parent = div.parentNode
            if (!callout.alwaysVisible) {
              callout.set('visible', !callout.visible)
            }
            if (callout.visible) {
              parent.removeChild(div)
              parent.appendChild(div)
            }
          }
          this.$trigger('markertap', {}, {
            markerId: option.id || 1
          })
        })
        markers.push(marker)
      })
    },
    changeMarker (marker, option) {
      var map = this.map
      var title = option.title || option.name
      var position = new maps.LatLng(option.latitude, option.longitude)
      var img = new Image()
      img.onload = () => {
        var anchor = option.anchor || {}
        var icon
        var w
        var h
        var top
        var x = anchor.x
        var y = anchor.y
        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
        }
        x = (typeof x === 'number' ? x : 0.5) * w
        y = (typeof y === 'number' ? y : 1) * h
        top = h - (h - y)
        icon = new maps.MarkerImage(img.src, null, null, new maps.Point(x, y), new maps.Size(w, h))
        // 修改标注-暂不支持透明度
        marker.setPosition(position)
        marker.setIcon(icon)
        marker.setRotation(option.rotate || 0)
        // 创建标注文本
        var labelOpt = option.label || {}
        if (marker.label) {
          marker.label.setMap(null)
          delete (marker.label)
        }
        var label
        if (labelOpt.content) {
          label = new maps.Label({
            position,
            map,
            clickable: false,
            content: labelOpt.content,
            style: {
              border: 'none',
              padding: '8px',
              background: 'none',
              color: labelOpt.color,
              fontSize: (labelOpt.fontSize || 14) + 'px',
              lineHeight: (labelOpt.fontSize || 14) + 'px',
              marginLeft: labelOpt.x,
              marginTop: labelOpt.y
            }
          })
          marker.label = label
        }
        // 创建标注气泡
        var calloutOpt = option.callout || {}
        var callout = marker.callout
        var calloutStyle
        if (calloutOpt.content) {
          calloutStyle = {
            id: option.id,
            position,
            map,
            top,
            content: calloutOpt.content,
            color: calloutOpt.color,
            fontSize: calloutOpt.fontSize,
            borderRadius: calloutOpt.borderRadius,
            bgColor: calloutOpt.bgColor,
            padding: calloutOpt.padding,
            boxShadow: calloutOpt.boxShadow,
            display: calloutOpt.display
          }
        } else if (title) {
          calloutStyle = {
            id: option.id,
            position,
            map,
            top,
            content: title,
            boxShadow: '0px 0px 3px 1px rgba(0,0,0,0.5)'
          }
        }
        if (calloutStyle) {
          if (callout) {
            callout.setOption(calloutStyle)
          } else {
            callout = marker.callout = new Callout(calloutStyle)
          }
        } else {
          if (callout) {
            callout.setMap(null)
            delete (marker.callout)
          }
        }
      }
601 602 603
      console.log(option.iconPath)
      console.log(this.$getRealPath(option.iconPath))
      img.src = this.$getRealPath(option.iconPath) ||
fxy060608's avatar
fxy060608 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
        'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAABQCAYAAABFyhZTAAANDElEQVR4nNWce4hc133Hv+fc92MeuytpV5ZXll2XuvTlUBTSP1IREsdNiKGEEAgE3EBLaBtK/2hNoQTStISUosiGOqVpQ+qkIdAax1FiG+oYIxyD4xi3uKlEXSFFke3d1e5od+a+H+ec/nHvmbkzs6ud2bmjTX7wY3b3zr3nfM7vd37n8Tt3CW6DiDP3EABSd/0KAEEuXBHzrsteFTiwVOBo+amUP9PK34ZuAcD30NoboTZgceYeCaQAUEvVAKiZ0lpiiv0Lgmi/imFLF5YV2SWFR1e0fGcDQF5qVn4y1Ag/E3DFmhJSB2Dk1D2Squ0HBdT3C0JPE6oco6oKqmm7PodnGXieQ3DWIYL/iCB/UWO95zTW2wCQlpqhgJ8J/MDApUUVFFY0AFiRdvwMJ8bvCaKcUW3bUE0DimGAKMpkz2QMLEnBkhhZEHICfoHy+AkrW3seQAwgQQHPyIUr/CD1nhq4tCpFAWoCsGNt5X2MWo9Qw/p1zXGgWiZAZu8teRQhCwLwOLpEefKolb3zDIAQBXyGAnwqa09Vq4pVDQBOqrTuTmn7c9S0H9QdB6ptT/O4iSWPY2S+DxYHFzTW+5zBti8BCFBYfCprTwxcwmoALABupK48lFPri0az1dSbjWkZDiSp5yPpdn2Vh39m5evPAPABRACySaH3Ba64sA7ABtD0tdXPUqvxKd1xoJrmDAjTSx7HCDsdroj0nJO99TiAHgprZwD4fi5+S+AKrAHA5UQ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w8GvVleWMo+XB6sbBnIznJ1s8a+9EwQ5rxyJ4pzjbd/P72xyuc1aTQLMNMHYS2oHrri2dM0QQNI0sWnrOL8eRf3vrkcRbB3n2xY2MEiP9NM88/ivD/N6PbTq2rIv5qtt8dRaGKaccwgh8E4Y5ne2xNMYb6B+tq9umQvwyDIyKDVxddw0VfH8jTjGZhzDVMWLDQNbGGzZzNW6wPwsXM05V7OR+fEmvn09CPiNKMKyi29jYN0Ag0BVe9+Vst/7w7OKnIEFKF6pMRdtrL3VxctMMOOoi2q2r5/LnWeF5vqK90gAGyTaXTy5ZAtpXRms5jIMjcq8LQwMnywIAVgrDVwuD+9K68oZ1dxcWcrcX+IfScHKwBRWfu9H8Xn2XSm3w8LAYHfEQ5F6TVGYWM6qYsy570q5Lf+mYSRH1QFwA8AGgJsooOXe7tzl/wGchYFKtBMCwAAAAABJRU5ErkJggg=='
    },
    removeMarkers (markers) {
      // 销毁全部标注
      for (var index = 0; index < markers.length; index++) {
        var marker = markers[index]
        if (marker.label) {
          marker.label.setMap(null)
        }
        if (marker.callout) {
          marker.callout.setMap(null)
        }
        marker.setMap(null)
        markers.splice(index--, 1)
      }
    },
    createPolyline () {
      var map = this.map
      var polyline = this.polylineSync
      this.removePolyline()
      // 创建新路线
      this.polyline.forEach(option => {
        var path = []
        option.points.forEach(point => {
          path.push(new maps.LatLng(point.latitude, point.longitude))
        })

        if (option.borderWidth) {
          var border = new maps.Polyline({
            map,
            clickable: false,
            path,
            strokeWeight: option.width + option.borderWidth,
            strokeColor: option.borderColor,
            strokeDashStyle: option.dottedLine ? 'dash' : 'solid'
          })
          polyline.push(border)
        }
        var line = new maps.Polyline({
          map,
          clickable: false,
          path,
          strokeWeight: option.width,
          strokeColor: option.color,
          strokeDashStyle: option.dottedLine ? 'dash' : 'solid'
        })
        polyline.push(line)
      })
    },
    removePolyline () {
      var polyline = this.polylineSync
      // 销毁全部路线
      polyline.forEach(line => {
        line.setMap(null)
      })
      polyline.splice(0, polyline.length)
    },
    createCircles () {
      var map = this.map
      var circles = this.circlesSync
      this.removeCircles()
      // 创建新圆形
      this.circles.forEach(option => {
        var center = new maps.LatLng(option.latitude, option.longitude)

        function getColor (color) {
          var c = color.match(/#[0-9A-Fa-f]{6}([0-9A-Fa-f]{2})?/)
          if (c && c.length) {
            return maps.Color.fromHex(c[0], Number('0x' + c[1] || 255) / 255)
          } else {
            return undefined
          }
        }
        var circle = new maps.Circle({
          map,
          center,
          clickable: false,
          radius: option.radius,
          strokeWeight: option.strokeWidth,
          fillColor: getColor(option.fillColor),
          strokeColor: getColor(option.color),
          strokeDashStyle: 'solid'
        })
        circles.push(circle)
      })
    },
    removeCircles () {
      var circles = this.circlesSync
      // 销毁全部圆形
      circles.forEach(circle => {
        circle.setMap(null)
      })
      circles.splice(0, circles.length)
    },
    createControls () {
      var map = this.map
      var controls = this.controlsSync
      this.removeControls()
      // 创建新控件
      this.controls.forEach(option => {
        var position = option.position || {}
        var control = document.createElement('div')
        var img = new Image()
        control.appendChild(img)
        var style = control.style
        style.position = 'absolute'
        style.width = 0
        style.height = 0
        img.onload = () => {
          if (option.position.width) {
            img.width = option.position.width
          }
          if (option.position.height) {
            img.height = option.position.height
          }
          var style = img.style
          style.position = 'absolute'
          style.left = (position.left || 0) + 'px'
          style.top = (position.top || 0) + 'px'
          style.maxWidth = 'initial'
        }
725
        img.src = this.$getRealPath(option.iconPath)
fxy060608's avatar
fxy060608 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
        onTap(img, $event => {
          if (option.clickable) {
            this.$trigger('controltap', $event, {
              controlId: option.id
            })
          }
        })
        map.controls[maps.ControlPosition.TOP_LEFT].push(control)
        controls.push(control)
      })
    },
    removeControls () {
      var controls = this.controlsSync
      // 销毁全部控件
      controls.forEach(control => {
        control.remove()
      })
      controls.splice(0, controls.length)
    },
    createLocation () {
      var map = this.map
      var location = this.mapLocation
      // 移除地点标注
      if (location) {
        this.removeLocation()
      }
      // 创建新地点标注
      uni.getLocation({
        type: 'gcj02',
        success: (res) => {
          if (location !== this.mapLocation) {
            return
          }
          var position = new maps.LatLng(res.latitude, res.longitude)
          location = new maps.Marker({
            position,
            map,
            icon: new maps.MarkerImage(
              'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIQAAACECAMAAABmmnOVAAAC01BMVEUAAAAAef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef96quGStdqStdpbnujMzMzCyM7Gyc7Ky83MzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMz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BgMBoPBYDAYDAYj8/APG67Rie8pUDsAAAAASUVORK5CYII=',
              null, null, new maps.Point(22, 22), new maps.Size(44, 44)),
            flat: true,
            rotation: 0
          })
          this.mapLocation = location
          refreshLocation()
          uni.onCompassChange(function (res) {
            location.setRotation(res.direction)
          })
        },
        fail: e => {
          console.error(e)
        }
      })
      var self = this

      function refreshLocation () {
        if (location !== self.mapLocation) {
          return
        }
        setTimeout(() => {
          uni.getLocation({
            type: 'gcj02',
            success: (res) => {
              var locationPosition = self.mapLocationPosition = new maps.LatLng(res.latitude, res.longitude)
              location.setPosition(locationPosition)
            },
            fail: e => {
              console.error(e)
            },
            complete: () => {
              refreshLocation()
            }
          })
        }, 1000)
      }
    },
    removeLocation () {
      var location = this.mapLocation
      if (location) {
        location.setMap(null)
        this.mapLocation = null
        this.mapLocationPosition = null
        uni.stopCompass()
      }
    },
    fitBounds (points, cb) {
      // 设置地图范围
      this.boundsReady(() => {
        var map = this.map
        var bounds = new maps.LatLngBounds()

        points.forEach(point => {
          var longitude = point.longitude
          var latitude = point.latitude
          var latLng = new maps.LatLng(latitude, longitude)
          bounds.extend(latLng)
        })
        map.fitBounds(bounds)
        if (typeof cb === 'function') {
          cb()
        }
      })
    },
    mapReady (cb) {
      if (this.isMapReady) {
        cb()
      } else {
        this.$once('mapready', () => {
          cb()
        })
      }
    },
    boundsReady (cb) {
      if (this.isBoundsReady) {
        cb()
      } else {
        this.$once('boundsready', () => {
          cb()
        })
      }
    },
    getMarker (id) {
      var markers = this.markersSync
      for (var index = 0; index < markers.length; index++) {
        var element = markers[index]
        if (element.id === id) {
          return element
        }
      }
    },
    createClass () {
      var self = this

      Callout.prototype = new maps.Overlay()
      Callout.prototype.init = function () {
        var option = this.option
        var div = this.div = document.createElement('div')
        var divStyle = div.style
        divStyle.position = 'absolute'
        divStyle.whiteSpace = 'nowrap'
        divStyle.transform = 'translateX(-50%) translateY(-100%)'
        divStyle.zIndex = 1
        divStyle.boxShadow = option.boxShadow || 'none'
        divStyle.display = this.visible ? 'block' : 'none'
        var triangle = this.triangle = document.createElement('div')
        triangle.setAttribute('style',
          'position: absolute;white-space: nowrap;border-width: 4px;border-style: solid;border-color: #fff transparent transparent;border-image: initial;font-size: 12px;padding: 0px;background-color: transparent;width: 0px;height: 0px;transform: translate(-50%, 100%);left: 50%;bottom: 0;'
        )
        this.setStyle(option)
        this.changed = function (key) {
          divStyle.display = this.visible ? 'block' : 'none'
        }
        div.appendChild(triangle)
        onTap(div, $event => {
          self.$trigger('callouttap', $event, {
            markerId: option.id || 1
          })
          $event.stopPropagation()
          $event.preventDefault()
        })
      }
      // 定义construct,实现这个接口来初始化自定义的Dom元素
      Callout.prototype.construct = function () {
        var div = this.div
        // 将dom添加到覆盖物层
        var panes = this.getPanes()
        // 设置panes的层级,overlayMouseTarget可接收点击事件
        panes.floatPane.appendChild(div)
      }
      // 实现draw接口来绘制和更新自定义的dom元素
      Callout.prototype.draw = function () {
        var overlayProjection = this.getProjection()
        if (!this.position || !this.div || !overlayProjection) {
          return
        }
        // 返回覆盖物容器的相对像素坐标
        var pixel = overlayProjection.fromLatLngToDivPixel(this.position)
        var divStyle = this.div.style
        divStyle.left = pixel.x + 'px'
        divStyle.top = pixel.y + 'px'
      }
      // 实现destroy接口来删除自定义的Dom元素,此方法会在setMap(null)后被调用
      Callout.prototype.destroy = function () {
        this.div.parentNode.removeChild(this.div)
        this.div = null
        this.triangle = null
      }
      Callout.prototype.setOption = function (option) {
        this.option = option
        this.setPosition(option.position)
        if (option.display === 'ALWAYS') {
          this.alwaysVisible = this.visible = true
        } else {
          this.alwaysVisible = false
        }
        this.setStyle(option)
      }
      Callout.prototype.setStyle = function (option) {
        var div = this.div
        var divStyle = div.style
        div.innerHTML = option.content
        divStyle.lineHeight = (option.fontSize || 14) + 'px'
        divStyle.fontSize = (option.fontSize || 14) + 'px'
        divStyle.padding = (option.padding || 8) + 'px'
        divStyle.color = option.color || '#000'
        divStyle.borderRadius = (option.borderRadius || 0) + 'px'
        divStyle.backgroundColor = option.bgColor || '#fff'
        divStyle.marginTop = '-' + (option.top + 5) + 'px'
        this.triangle.style.borderColor = `${option.bgColor || '#fff'} transparent transparent`
      }
      Callout.prototype.setPosition = function (position) {
        this.position = position
        this.draw()
      }
    },
    loadMap (callback) {
      // 加载腾讯地图js
      if ('qq' in window && typeof window.qq === 'object' && 'maps' in window.qq) {
        maps = window.qq.maps
        callback()
      } else {
        let key = 'JMRBZ-R4HCD-X674O-PXLN4-B7CLH-42BSB'
        let callbackName = '_callback' + Date.now()
        window[callbackName] = function () {
          delete window[callbackName]
          maps = window.qq.maps
          callback()
        }
        let script = document.createElement('script')
        script.type = 'text/javascript'
        script.src = `https://map.qq.com/api/js?v=2.exp&key=${key}&callback=${callbackName}&libraries=geometry`
        document.body.appendChild(script)
      }
    }
  }
}
</script>

<style>
uni-map {
  display: block;
}
</style>