create-map-context.js 1.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
import {
  invokeMethod,
  getCurrentPageVm
4
} from '../../platform'
5 6 7 8

import {
  callback
} from 'uni-shared'
fxy060608's avatar
fxy060608 已提交
9 10 11 12 13

function operateMapPlayer (mapId, pageVm, type, data) {
  invokeMethod('operateMapPlayer', mapId, pageVm, type, data)
}

14 15 16 17 18
UniServiceJSBridge.subscribe('onMapMethodCallback', ({
  callbackId,
  data
}) => {
  callback.invoke(callbackId, data)
19 20
})

d-u-a's avatar
d-u-a 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
const methods = ['getCenterLocation',
  'moveToLocation',
  'getScale',
  'getRegion',
  'includePoints',
  'translateMarker',
  'addCustomLayer',
  'removeCustomLayer',
  'addGroundOverlay',
  'removeGroundOverlay',
  'updateGroundOverlay',
  'initMarkerCluster',
  'addMarkers',
  'removeMarkers',
  'moveAlong',
  'openMapApp']
37

Q
qiang 已提交
38
export class MapContext {
fxy060608's avatar
fxy060608 已提交
39 40 41
  constructor (id, pageVm) {
    this.id = id
    this.pageVm = pageVm
d-u-a's avatar
d-u-a 已提交
42 43 44 45 46 47 48
  }

  on (name, callback) {
    operateMapPlayer(this.id, this.pageVm, 'on', {
      name,
      callback
    })
fxy060608's avatar
fxy060608 已提交
49
  }
50 51 52 53 54 55
}

MapContext.prototype.$getAppMap = function () {
  if (__PLATFORM__ === 'app-plus') {
    return plus.maps.getMapById(this.pageVm.$page.id + '-map-' + this.id)
  }
d-u-a's avatar
d-u-a 已提交
56
}
fxy060608's avatar
fxy060608 已提交
57

58
methods.forEach(function (method) {
59
  MapContext.prototype[method] = callback.warp(function (options, callbackId) {
60 61
    options.callbackId = callbackId
    operateMapPlayer(this.id, this.pageVm, method, options)
62
  })
63
})
fxy060608's avatar
fxy060608 已提交
64 65 66 67 68 69

export function createMapContext (id, context) {
  if (context) {
    return new MapContext(id, context)
  }
  return new MapContext(id, getCurrentPageVm('createMapContext'))
Q
qiang 已提交
70
}