index.vue 12.2 KB
Newer Older
d-u-a's avatar
d-u-a 已提交
1
<template>
Q
qiang 已提交
2
  <uni-map v-on="$listeners">
d-u-a's avatar
d-u-a 已提交
3 4
    <div
      ref="container"
fxy060608's avatar
fxy060608 已提交
5 6
      class="uni-map-container"
    />
d-u-a's avatar
d-u-a 已提交
7 8 9 10 11
    <v-uni-cover-image
      v-for="(control, index) in mapControls"
      :key="index"
      :src="control.iconPath"
      :style="control.position"
12
      auto-size
fxy060608's avatar
fxy060608 已提交
13 14
      @click="controlclick(control)"
    />
d-u-a's avatar
d-u-a 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    <div class="uni-map-slot">
      <slot />
    </div>
  </uni-map>
</template>
<script>
import {
  subscriber
} from 'uni-mixins'
import native from '../../mixins/native'

const methods = [
  'getCenterLocation',
  'moveToLocation',
  'getRegion',
  'getScale',
  '$getAppMap'
]

d-u-a's avatar
d-u-a 已提交
34 35 36 37 38 39 40 41
// const events = [
//   'markertap',
//   'callouttap',
//   'controltap',
//   'regionchange',
//   'tap',
//   'updated'
// ]
d-u-a's avatar
d-u-a 已提交
42 43 44 45 46 47 48

const attrs = [
  'latitude',
  'longitude',
  'scale',
  'markers',
  'polyline',
49
  'polygons',
d-u-a's avatar
d-u-a 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  'circles',
  'controls',
  'show-location'
]

const convertCoordinates = (lng, lat, callback) => {
  // plus.maps.Map.convertCoordinates(new plus.maps.Point(lng, lat), {
  //   coordType: 'gcj02'
  // }, callback)
  callback({
    coord: {
      latitude: lat,
      longitude: lng
    }
  })
}

d-u-a's avatar
d-u-a 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80
function parseHex (color) {
  if (color.indexOf('#') !== 0) {
    return {
      color,
      opacity: 1
    }
  }
  const opacity = color.substr(7, 2)
  return {
    color: color.substr(0, 7),
    opacity: opacity ? Number('0x' + opacity) / 255 : 1
  }
}

d-u-a's avatar
d-u-a 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
export default {
  name: 'Map',
  mixins: [subscriber, native],
  props: {
    id: {
      type: String,
      default: ''
    },
    latitude: {
      type: [Number, String],
      default: ''
    },
    longitude: {
      type: [Number, String],
      default: ''
    },
    scale: {
      type: [String, Number],
99
      default: 16
d-u-a's avatar
d-u-a 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    },
    markers: {
      type: Array,
      default () {
        return []
      }
    },
    polyline: {
      type: Array,
      default () {
        return []
      }
    },
    circles: {
      type: Array,
      default () {
        return []
      }
    },
119 120 121 122 123 124
    polygons: {
      type: Array,
      default () {
        return []
      }
    },
d-u-a's avatar
d-u-a 已提交
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
    controls: {
      type: Array,
      default () {
        return []
      }
    }
  },
  data () {
    return {
      style: {
        top: '0px',
        left: '0px',
        width: '0px',
        height: '0px',
        position: 'static'
      },
      hidden: false
    }
  },
  computed: {
    attrs () {
      const obj = {}
      attrs.forEach(key => {
        let val = this.$props[key]
        val = key === 'src' ? this.$getRealPath(val) : val
        obj[key.replace(/[A-Z]/g, str => '-' + str.toLowerCase())] = val
      })
      return obj
    },
    mapControls () {
      const list = this.controls.map((control) => {
fxy060608's avatar
fxy060608 已提交
156
        const position = { position: 'absolute' };
d-u-a's avatar
d-u-a 已提交
157
        ['top', 'left', 'width', 'height'].forEach(key => {
158 159 160
          if (control.position[key]) {
            position[key] = control.position[key] + 'px'
          }
d-u-a's avatar
d-u-a 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173
        })
        return {
          id: control.id,
          iconPath: this.$getRealPath(control.iconPath),
          position: position
        }
      })
      return list
    }
  },
  watch: {
    hidden (val) {
      this.map && this.map[val ? 'hide' : 'show']()
174
    },
175
    scale (val) {
176
      this.map && this.map.setZoom(parseInt(val))
177
    },
178 179 180 181 182 183 184 185 186 187 188
    latitude (val) {
      this.map && this.map.setStyles({
        center: new plus.maps.Point(this.longitude, this.latitude)
      })
    },
    longitude (val) {
      this.map && this.map.setStyles({
        center: new plus.maps.Point(this.longitude, this.latitude)
      })
    },
    markers (val) {
189
      this.map && this._addMarkers(val, true)
190 191 192 193 194 195
    },
    polyline (val) {
      this.map && this._addMapLines(val)
    },
    circles (val) {
      this.map && this._addMapCircles(val)
196 197 198
    },
    polygons (val) {
      this.map && this._addMapPolygons(val)
d-u-a's avatar
d-u-a 已提交
199 200 201
    }
  },
  mounted () {
D
DCloud_LXH 已提交
202 203 204 205 206 207 208 209 210 211
    this._onParentReady(() => {
      const mapStyle = Object.assign({}, this.attrs, this.position)
      if (this.latitude && this.longitude) {
        mapStyle.center = new plus.maps.Point(this.longitude, this.latitude)
      }
      const map = this.map = plus.maps.create(this.$page.id + '-map-' + (this.id || Date.now()), mapStyle)
      map.__markers__ = []
      map.__markers_map__ = {}
      map.__lines__ = []
      map.__circles__ = []
212
      map.__polygons__ = []
D
DCloud_LXH 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
      map.setZoom(parseInt(this.scale))
      plus.webview.currentWebview().append(map)
      if (this.hidden) {
        map.hide()
      }
      this.$watch('position', () => {
        this.map && this.map.setStyles(this.position)
      }, {
        deep: true
      })
      map.onclick = (e) => {
        this.$trigger('click', {}, e)
      }
      map.onstatuschanged = (e) => {
        this.$trigger('regionchange', {}, {})
      }
      this._addMarkers(this.markers)
      this._addMapLines(this.polyline)
      this._addMapCircles(this.circles)
232
      this._addMapPolygons(this.polygons)
d-u-a's avatar
d-u-a 已提交
233 234 235
    })
  },
  beforeDestroy () {
236
    this.map && this.map.close()
d-u-a's avatar
d-u-a 已提交
237 238 239 240 241 242 243 244 245 246 247 248
    delete this.map
  },
  methods: {
    _handleSubscribe ({
      type,
      data = {}
    }) {
      if (!methods.includes(type)) {
        return
      }
      this.map && this[type](data)
    },
249 250 251 252 253
    moveToLocation ({ callbackId, longitude, latitude }) {
      this.map.setCenter(new plus.maps.Point(longitude || this.longitude, latitude || this.latitude))
      this._publishHandler(callbackId, {
        errMsg: 'moveToLocation:ok'
      })
d-u-a's avatar
d-u-a 已提交
254
    },
255
    getCenterLocation ({ callbackId }) {
256 257 258 259 260 261
      this.map.getCurrentCenter((state, point) => {
        this._publishHandler(callbackId, {
          longitude: point.longitude,
          latitude: point.latitude,
          errMsg: 'getCenterLocation:ok'
        })
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
      })
    },
    getRegion ({ callbackId }) {
      const rect = this.map.getBounds()
      this._publishHandler(callbackId, {
        southwest: rect.southwest,
        northeast: rect.northeast || rect.northease, // 5plus API 名字写错了
        errMsg: 'getRegion:ok'
      })
    },
    getScale ({ callbackId }) {
      this._publishHandler(callbackId, {
        scale: this.map.getZoom(),
        errMsg: 'getScale:ok'
      })
d-u-a's avatar
d-u-a 已提交
277 278
    },
    controlclick (e) {
279
      this.$trigger('controltap', {}, { controlId: e.id })
d-u-a's avatar
d-u-a 已提交
280
    },
281 282 283 284 285 286
    _publishHandler (callbackId, data) {
      UniViewJSBridge.publishHandler('onMapMethodCallback', {
        callbackId,
        data
      }, this.$page.id)
    },
d-u-a's avatar
d-u-a 已提交
287 288 289
    _addMarker (nativeMap, marker) {
      const {
        id,
d-u-a's avatar
d-u-a 已提交
290
        // title,
d-u-a's avatar
d-u-a 已提交
291 292 293
        latitude,
        longitude,
        iconPath,
d-u-a's avatar
d-u-a 已提交
294 295 296 297
        // width,
        // height,
        // rotate,
        // alpha,
d-u-a's avatar
d-u-a 已提交
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
        callout,
        label
      } = marker
      convertCoordinates(longitude, latitude, res => {
        const {
          latitude,
          longitude
        } = res.coord
        const nativeMarker = new plus.maps.Marker(new plus.maps.Point(longitude, latitude))
        if (iconPath) {
          nativeMarker.setIcon(this.$getRealPath(iconPath))
        }
        if (label && label.content) {
          nativeMarker.setLabel(label.content)
        }
        let nativeBubble = false
        if (callout && callout.content) {
          nativeBubble = new plus.maps.Bubble(callout.content)
        }
        if (nativeBubble) {
          nativeMarker.setBubble(nativeBubble)
        }
        if (id || id === 0) {
          nativeMarker.onclick = (e) => {
            this.$trigger('markertap', {}, {
323
              markerId: id
d-u-a's avatar
d-u-a 已提交
324 325 326 327 328
            })
          }
          if (nativeBubble) {
            nativeBubble.onclick = () => {
              this.$trigger('callouttap', {}, {
329
                markerId: id
d-u-a's avatar
d-u-a 已提交
330 331 332 333 334
              })
            }
          }
        }
        nativeMap.addOverlay(nativeMarker)
335 336
        nativeMap.__markers__.push(nativeMarker)
        nativeMap.__markers_map__[id + ''] = nativeMarker
d-u-a's avatar
d-u-a 已提交
337 338
      })
    },
339 340
    _clearMarkers () {
      const map = this.map
341 342 343 344 345 346
      const markers = map.__markers__
      markers.forEach(marker => {
        map.removeOverlay(marker)
      })
      map.__markers__ = []
      map.__markers_map__ = {}
347
    },
d-u-a's avatar
d-u-a 已提交
348
    _addMarkers (markers, clear) {
349 350
      if (clear) {
        this._clearMarkers()
d-u-a's avatar
d-u-a 已提交
351
      }
352 353 354
      markers.forEach(marker => {
        this._addMarker(this.map, marker)
      })
d-u-a's avatar
d-u-a 已提交
355 356 357 358 359 360 361 362
    },
    _translateMapMarker ({
      autoRotate,
      callbackId,
      destination,
      duration,
      markerId
    }) {
363 364 365
      const nativeMarker = this.map.__markers_map__[markerId + '']
      if (nativeMarker) {
        nativeMarker.setPoint(new plus.maps.Point(destination.longitude, destination.latitude))
d-u-a's avatar
d-u-a 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
      }
    },
    _addMapLines (lines) {
      const nativeMap = this.map

      if (nativeMap.__lines__.length > 0) {
        nativeMap.__lines__.forEach(circle => {
          nativeMap.removeOverlay(circle)
        })
        nativeMap.__lines__ = []
      }

      lines.forEach(line => {
        const {
          color,
d-u-a's avatar
d-u-a 已提交
381 382 383 384 385 386
          width
          // dottedLine,
          // arrowLine,
          // arrowIconPath,
          // borderColor,
          // borderWidth
d-u-a's avatar
d-u-a 已提交
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
        } = line
        const points = line.points.map(point => new plus.maps.Point(point.longitude, point.latitude))
        const polyline = new plus.maps.Polyline(points)
        if (color) {
          const strokeStyle = parseHex(color)
          polyline.setStrokeColor(strokeStyle.color)
          polyline.setStrokeOpacity(strokeStyle.opacity)
        }
        if (width) {
          polyline.setLineWidth(width)
        }
        nativeMap.addOverlay(polyline)
        nativeMap.__lines__.push(polyline)
      })
    },
    _addMapCircles (circles) {
      const nativeMap = this.map

      if (nativeMap.__circles__.length > 0) {
        nativeMap.__circles__.forEach(circle => {
          nativeMap.removeOverlay(circle)
        })
        nativeMap.__circles__ = []
      }

      circles.forEach(circle => {
        const {
          latitude,
          longitude,
          color,
          fillColor,
          radius,
          strokeWidth
        } = circle
        const nativeCircle = new plus.maps.Circle(new plus.maps.Point(longitude, latitude), radius)
        if (color) {
          const strokeStyle = parseHex(color)
          nativeCircle.setStrokeColor(strokeStyle.color)
          nativeCircle.setStrokeOpacity(strokeStyle.opacity)
        }
        if (fillColor) {
          const fillStyle = parseHex(fillColor)
          nativeCircle.setFillColor(fillStyle.color)
          nativeCircle.setFillOpacity(fillStyle.opacity)
        }
        if (strokeWidth) {
          nativeCircle.setLineWidth(strokeWidth)
        }
        nativeMap.addOverlay(nativeCircle)
        nativeMap.__circles__.push(nativeCircle)
      })
438 439 440 441 442
    },
    _addMapPolygons (polygons) {
      const nativeMap = this.map

      const nativeMapPolygons = nativeMap.__polygons__
d-u-a's avatar
d-u-a 已提交
443
      nativeMapPolygons.forEach(polygon => {
444 445 446 447 448 449 450 451 452
        nativeMap.removeOverlay(polygon)
      })
      nativeMapPolygons.length = 0

      polygons.forEach(polygon => {
        const {
          points,
          strokeWidth,
          strokeColor,
d-u-a's avatar
d-u-a 已提交
453
          fillColor
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
        } = polygon
        const plusPoints = []
        if (points) {
          points.forEach(({ latitude, longitude }) => {
            plusPoints.push(new plus.maps.Point(longitude, latitude))
          })
        }
        const nativePolygon = new plus.maps.Polygon(plusPoints)
        if (strokeColor) {
          const strokeStyle = parseHex(strokeColor)
          nativePolygon.setStrokeColor(strokeStyle.color)
          nativePolygon.setStrokeOpacity(strokeStyle.opacity)
        }
        if (fillColor) {
          const fillStyle = parseHex(fillColor)
          nativePolygon.setFillColor(fillStyle.color)
          nativePolygon.setFillOpacity(fillStyle.opacity)
        }
        if (strokeWidth) {
          nativePolygon.setLineWidth(strokeWidth)
        }
        nativeMap.addOverlay(nativePolygon)
        nativeMapPolygons.push(nativePolygon)
      })
d-u-a's avatar
d-u-a 已提交
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
    }
  }
}
</script>

<style>
  uni-map {
    width: 300px;
    height: 225px;
    display: inline-block;
    line-height: 0;
    overflow: hidden;
    position: relative;
  }

  uni-map[hidden] {
    display: none;
  }

  .uni-map-container {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    overflow: hidden;
    background-color: black;
  }

  .uni-map-slot {
    position: absolute;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    pointer-events: none;
  }
</style>