map.js 2.0 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
import {
  invoke
} from '../../bridge'

export function getMapCenterLocation ({
  mapId
} = {}, callbackId) {
  const nativeMap = plus.maps.getMapById(mapId + '')
  if (nativeMap) {
    nativeMap.getCurrentCenter((state, {
      latitude,
      longitude
    } = {}) => {
      if (state === 0) {
        invoke(callbackId, {
          latitude,
          longitude,
          errMsg: 'getMapCenterLocation:ok'
        })
      } else {
        invoke(callbackId, {
          errMsg: 'getMapCenterLocation:fail:state[' + state + ']'
        })
      }
    })
  } else {
    return {
      errMsg: 'getMapCenterLocation:fail:地图[' + mapId + ']不存在'
    }
  }
}

export function moveToMapLocation ({
  mapId
} = {}) {
  const nativeMap = plus.maps.getMapById(mapId + '')
  if (nativeMap) {
    nativeMap.getUserLocation((state, {
      latitude,
      longitude
    } = {}) => {
      if (state === 0) {
        nativeMap.setCenter(new plus.maps.Point(longitude, latitude))
      }
    })
  }
  return {
    errMsg: 'moveToMapLocation:ok'
  }
}

export function getMapScale ({
  mapId
} = {}) {
  const nativeMap = plus.maps.getMapById(mapId + '')
  if (nativeMap) {
    return {
      scale: nativeMap.getZoom(),
      errMsg: 'getMapScale:ok'
    }
  }
  return {
    errMsg: 'getMapScale:fail:地图[' + mapId + ']不存在'
  }
}

export function getMapRegion ({
  mapId
} = {}) {
  const nativeMap = plus.maps.getMapById(mapId + '')
  if (nativeMap) {
    const bounds = nativeMap.getBounds()
    const northeast = bounds.getNorthEase()
    const southwest = bounds.getSouthWest()
    return {
      northeast: {
        latitude: northeast.getLat(),
        longitude: northeast.getLng()
      },
      southwest: {
        latitude: southwest.getLat(),
        longitude: southwest.getLng()
      },
      errMsg: 'getMapRegion:ok'
    }
  }
  return {
    errMsg: 'getMapRegion:fail:地图[' + mapId + ']不存在'
  }
}