OnlineMap.vue 4.63 KB
Newer Older
呼呼啦啦 committed
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
<template>
  <el-container
    v-loading="loading"
    class="online-map-container"
  >
    <el-aside class="aside-form">
      <div class="form-title">
        <span>{{ $t('online_map.onlinemap') }}</span>
      </div>

      <div class="online-form-item">
        <span class="form-label">Key</span>
        <el-input
          v-model="key"
          size="small"
        />
      </div>
      <el-button
        type="primary"
        :disabled="!key"
        @click="saveHandler"
      >{{ $t('commons.save') }}</el-button>
    </el-aside>
    <el-divider
      direction="vertical"
      class="online-divider"
    />
    <el-main class="main-content">
      <div
        v-show="mapLoaded"
        v-if="!mapReloading"
        :id="domId"
        class="map-gaode-demo"
      />
      <el-empty
        v-if="!mapLoaded"
        :description="$t('online_map.empty_desc')"
      />
    </el-main>
  </el-container>
</template>
<script>
import { saveMapKey, queryMapKey } from '@/api/map/map'
export default {
  name: 'OnlineMap',
  data() {
    return {
      key: '',
      secret: '',
      mapLoaded: false,
      mapInstance: null,
      domId: 'qwertyuiop',
      mapReloading: false,
      loading: false
    }
  },
  mounted() {
    this.initLoad()
  },

  methods: {
    loadMap() {
      if (!this.key) {
        return
      }
      const mykey = this.key
      const url = `https://webapi.amap.com/maps?v=2.0&key=${mykey}`

      this.loadScript(url).then(res => {
        if (this.mapInstance) {
          this.mapInstance.destroy()
          this.mapInstance = null
          this.mapReloading = true
          setTimeout(() => {
            this.domId = this.domId + '-de-'
            this.mapReloading = false
            this.$nextTick(() => {
              this.createMapInstance()
            })
          }, 1500)

          return
        }
        this.createMapInstance()
      }).catch(e => {
        if (this.mapInstance) {
          this.mapInstance.destroy()
          this.mapInstance = null
        }
        console.error(e)
      })
    },
    createMapInstance() {
      this.mapInstance = new window.AMap.Map(this.domId, {
        viewMode: '2D',
        zoom: 11,
        center: [116.397428, 39.90923]
      })
      this.mapLoaded = true
    },
    saveHandler() {
      this.loading = true
      saveMapKey({ key: this.key }).then(() => {
        this.$message({
          message: this.$t('commons.save_success'),
          type: 'success',
          showClose: true
        })
        this.initLoad()
      }).catch(e => {
        console.error(e)
      }).finally(() => {
        this.loading = false
      })
    },
    initLoad() {
      console.log('map load ...')
      queryMapKey().then(res => {
        this.key = res.data
        this.loadMap()
      }).catch(e => {
        console.error(e)
      })
    },
    loadScript(url) {
      return new Promise(function(resolve, reject) {
        const scriptId = 'de-gaode-script-id'
        let dom = document.getElementById(scriptId)
        if (dom) {
          dom.parentElement.removeChild(dom)
          dom = null
          window.AMap = null
          // return resolve()
        }
        var script = document.createElement('script')

        script.id = scriptId
        script.onload = function() {
          return resolve()
        }
        script.onerror = function() {
          return reject(new Error('Load script from '.concat(url, ' failed')))
        }
        script.src = url
        var head = document.head || document.getElementsByTagName('head')[0];
        (document.body || head).appendChild(script)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.online-map-container {
    height: 100%;
    .online-divider {
      height: calc(100% - 139px);
      position: absolute;
      top: 112px;
      margin: 0 0 0 330px;
    }
    .aside-form {
        width: 320px !important;
        padding: 0 10px;
        height: 100%;
        .form-title {
            font-size: 16px;
            font-weight: 500;
            line-height: 24px;
            color: #1F2329;
            margin-bottom: 10px;
        }
        .online-form-item {
            font-size: 14px;
            .form-title {
                font-weight: 400;
                color: 1F2329;
                line-height: 22px;
            }
            height: 62px;
            margin-bottom: 10px;
        }
    }
    .main-content {
        width: 100%;
        height: 100%;
        margin-left: 13px;
        .map-gaode-demo {
            width: 100%;
            height: 100%;
            .de-map-iframe {
                width: 100%;
                height: 100%;
                border: none;
            }
        }
    }
}
</style>