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

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 19 20
UniServiceJSBridge.subscribe('onMapMethodCallback', ({
  callbackId,
  data
}) => {
  callback.invoke(callbackId, data)
})

21
const methods = ['getCenterLocation', 'moveToLocation', 'getScale', 'getRegion', 'includePoints', 'translateMarker']
22

Q
qiang 已提交
23
export class MapContext {
fxy060608's avatar
fxy060608 已提交
24 25 26 27
  constructor (id, pageVm) {
    this.id = id
    this.pageVm = pageVm
  }
28
}
29

fxy060608's avatar
fxy060608 已提交
30 31
MapContext.prototype.$getAppMap = function () {
  return plus.maps.getMapById(this.pageVm.$page.id + '-map-' + this.id)
32
}
fxy060608's avatar
fxy060608 已提交
33

34
methods.forEach(function (method) {
35 36 37
  MapContext.prototype[method] = callback.warp(function (options, callbackId) {
    options.callbackId = callbackId
    operateMapPlayer(this.id, this.pageVm, method, options)
38 39
  })
})
fxy060608's avatar
fxy060608 已提交
40 41 42 43 44 45

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