husq
2023-09-14 aad9909103473de08732b25c61c28e02f9835c9c
src/hooks/use-g-map.ts
@@ -4,30 +4,40 @@
export function useGMapManage () {
  const state = reactive({
    mapEntity: null,
    mapObj: null,
    aMap: null, // Map类
    map: null, // 地图对象
    mouseTool: null,
  })
  async function initMap (container: string, app:App) {
    AMapLoader.load({
      ...AMapConfig
    }).then((AMap) => {
      state.mapObj = AMap
      state.mapEntity = new AMap.Map(container, {
        center: [113.935913, 22.525335],
        zoom: 15
    return new Promise((resolve, reject) => {
      AMapLoader.load({
        ...AMapConfig
      }).then((AMap) => {
        console.log(AMap, 'AMap')
        state.aMap = AMap
        state.map = new AMap.Map(container, {
          center: [113.943225499, 22.577673716],
          zoom: 15
        })
        state.mouseTool = new AMap.MouseTool(state.map)
        // 挂在到全局
        app.config.globalProperties.$aMap = state.aMap
        app.config.globalProperties.$map = state.map
        app.config.globalProperties.$mouseTool = state.mouseTool
        resolve(state.map)
      }).catch(e => {
        console.log(e)
        reject(e)
      })
      state.mouseTool = new AMap.MouseTool(state.mapEntity)
      app.config.globalProperties.$aMap = state.mapEntity
      app.config.globalProperties.$aMapObj = state.mapObj
      app.config.globalProperties.$mouseTool = state.mouseTool
    }).catch(e => {
      console.log(e)
    })
  }
  function globalPropertiesConfig (app:App) {
    initMap('g-container', app)
  }
  return {
    globalPropertiesConfig,
  }