Map.vue 13.4 KB
Newer Older
yma16's avatar
yma16 已提交
1 2 3 4 5 6
/* eslint-disable eqeqeq */
<template>
  <div class="container">
    <div class="mapClass" id="echartMapid"></div>
    <div class="boxRight">
      <div class="refreshBtn">
yma16's avatar
yma16 已提交
7
        <el-button @click="refreshEchart">刷新</el-button>
yma16's avatar
yma16 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
      </div>
      <div class="selectPoint">
        <p>终点选择</p>
        <el-select v-model="focusCity" placeholder="终点选择">
          <el-option
            v-for="item in aimPointData"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            style="position: relative; overflow: hidden"
          >
          </el-option>
        </el-select>
        <p>地图点击</p>
        <el-input placeholder="请输入内容" v-model="clickCity" :disabled="true">
        </el-input>
      </div>
    </div>
  </div>
</template>

<script>
yma16's avatar
yma16 已提交
30
import * as echarts from 'echarts'
yma16's avatar
yma16 已提交
31

yma16's avatar
yma16 已提交
32
export default {
yma16's avatar
yma16 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    name: 'Map',
    data () {
        return {
            msg: 'map组件内层',
            option: {},
            // gisData
            gisData: null,
            // 热力层
            airData: [],
            mapEchart: null,
            // 飞线层
            linesCoord: [],
            // 中心层坐标,默认贵阳市
            centerLoction: [106.598978, 26.916134],
            // 气泡层
            locationGis: [],
            // 城市
            focusCity: '贵阳市',
            clickCity: '未选择',
            // 坐标
            focusGeo: null,
            // 终点选择
            aimPointData: []
        }
yma16's avatar
yma16 已提交
57
    },
yma16's avatar
yma16 已提交
58 59 60 61 62 63
    watch: {
        focusCity: function (oldValue, newValue) {
            console.log(oldValue, newValue)
            // this.mapEchart.clear()
            this.initOptionData()
        }
yma16's avatar
yma16 已提交
64
    },
yma16's avatar
yma16 已提交
65 66 67
    mounted () {
        this.initDomDiv()
        this.initOptionData()
yma16's avatar
yma16 已提交
68
    },
yma16's avatar
yma16 已提交
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
    methods: {
        initDomDiv: function () {
            if (!echarts) return
            let mapDom = document.getElementById('echartMapid')
            this.mapEchart = echarts.init(mapDom)
            // 显示加载
            this.mapEchart.showLoading({
                text: '初始化'
            })
            console.log('dom', this.mapEchart)
            try {
                console.log(
                    'domMain',
                    document.getElementById('mainappid'),
                    document.getElementById('mainappid').style
                )
                document.getElementById('mainappid').style.padding = '0'
                document.getElementById('mainappid').style.margin = '0'
            } catch (e) {
                console.log('e', e)
            }
        },
        refreshEchart: function () {
            this.mapEchart.clear()
            this.mapEchart.showLoading({
                text: '刷新中'
                // color: 'blue',
                // opacity: 0.2
            })
            this.initOptionData()
        },
        initOptionData: function () {
yma16's avatar
yma16 已提交
101 102 103
            // const echarts = this.$echarts
            // if (!echarts) return
            const that = this
yma16's avatar
yma16 已提交
104 105
            // 置空
            that.linesCoord = []
yma16's avatar
yma16 已提交
106
            async function task1 () {
yma16's avatar
yma16 已提交
107
                return new Promise((resolve) => {
yma16's avatar
yma16 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                    that.$axios
                        .get('https://yongma16.xyz/static/json/map/guizhou.json')
                        .then(function (response) {
                            let res = response.data
                            that.gisData = res
                            console.log('贵州数据', res)
                            let itemData = res.features
                            let length = itemData.length
                            that.aimPointData = []
                            for (let loc = 0; loc < length; ++loc) {
                                let name = itemData[loc].properties.name
                                that.aimPointData.push({
                                    value: name
                                })
                                let center = itemData[loc].properties.center
                                // 中心位置
                                if (name === that.focusCity) {
                                    that.centerLoction = center
yma16's avatar
yma16 已提交
126
                                }
yma16's avatar
yma16 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
                            }
                            for (let loc = 0; loc < length; ++loc) {
                                let name = itemData[loc].properties.name
                                let number = (Math.random() * 100).toFixed(2)
                                let center = itemData[loc].properties.center
                                that.locationGis.push({
                                    value: center
                                })
                                // eslint-disable-next-line eqeqeq
                                if (name !== that.focusCity) {
                                    that.linesCoord.push([center, that.centerLoction])
                                }
                                // eslint-disable-next-line eqeqeq
                                if (name && name !== '') {
                                    let temp = {
                                        name: `${name}`,
                                        value: Number(number)
yma16's avatar
yma16 已提交
144
                                    }
yma16's avatar
yma16 已提交
145
                                    that.airData.push(temp)
yma16's avatar
yma16 已提交
146
                                }
yma16's avatar
yma16 已提交
147 148 149 150 151 152 153 154
                                continue
                            }
                            console.log('测试热力处理', that.airData)
                            echarts.registerMap('geoJson', res)
                            resolve('第一个任务')
                        }).catch(r => {
                            resolve(r)
                        })
yma16's avatar
yma16 已提交
155 156
                })
            }
yma16's avatar
yma16 已提交
157

yma16's avatar
yma16 已提交
158
            async function task2 () {
yma16's avatar
yma16 已提交
159
                return new Promise((resolve) => {
yma16's avatar
yma16 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173
                    that.option = {
                        title: {
                            text: '贵州省地图',
                            subtext: 'geoJson'
                        },
                        geo: {
                            // 经纬度中心
                            center: that.centerLoction,
                            type: 'map',
                            map: 'geoJson', // 这里的值要和上面registerMap的第一个参数一致
                            roam: true, // 拖拽
                            nameProperty: 'name',
                            // 悬浮标签
                            label: {
yma16's avatar
yma16 已提交
174 175
                                type: 'map',
                                map: 'geoJson', // 这里的值要和上面registerMap的第一个参数一致
yma16's avatar
yma16 已提交
176
                                // roam: false, // 拖拽
yma16's avatar
yma16 已提交
177
                                nameProperty: 'name',
yma16's avatar
yma16 已提交
178 179 180 181 182 183 184 185 186 187 188 189
                                show: true,
                                color: '#fff',
                                backgroundColor: '#546de5',
                                align: 'center',
                                fontSize: 10,
                                width: (function () {
                                    // let n = parseInt(Math.random() * 10)
                                    return 110
                                })(),
                                height: 50,
                                shadowColor: 'rgba(0,0,0,.7)',
                                borderRadius: 10
yma16's avatar
yma16 已提交
190
                            },
yma16's avatar
yma16 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                            zoom: 1.2
                        },
                        series: [
                            // 坐标点的热力数据
                            {
                                data: that.airData,
                                geoIndex: 0, // 将热力的数据和第0个geo配置关联在一起
                                type: 'map'
                            },
                            {
                                type: 'effectScatter',
                                // 渲染显示
                                zlevel: 2,
                                showEffectOn: 'render',
                                data: that.locationGis, // 配置散点的坐标数据
                                coordinateSystem: 'geo', // 指明散点使用的坐标系统
                                rippleEffect: {
                                    // 缩放
                                    scale: 4,
                                    // 涟漪的颜色
                                    color: '#cf6a87',
                                    // 波纹数量
                                    number: 2,
                                    // 扩散方式 stroke(线条) fill(区域覆盖)
                                    brushType: 'fill'
yma16's avatar
yma16 已提交
216
                                },
yma16's avatar
yma16 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
                                // 形状
                                symbol: 'circle'
                            },
                            // 飞线层
                            {
                                // name: '贵阳市飞线',
                                type: 'lines',
                                coordinateSystem: 'geo',
                                polyline: true,
                                zlevel: 3,
                                effect: {
                                    show: true,
                                    period: 5,
                                    trailLength: 0, // 拖尾
                                    symbol: 'arrow', // 箭头
                                    color: 'red', // 样式颜色
                                    symbolSize: 5
yma16's avatar
yma16 已提交
234
                                },
yma16's avatar
yma16 已提交
235 236 237 238 239 240 241
                                lineStyle: {
                                    color: '#000',
                                    width: 2,
                                    type: 'solid',
                                    dashOffset: 0
                                    // cap: butt,
                                    // join: bevel
yma16's avatar
yma16 已提交
242
                                },
yma16's avatar
yma16 已提交
243 244
                                // 飞线层数据
                                data: that.linesCoord
yma16's avatar
yma16 已提交
245
                            }
yma16's avatar
yma16 已提交
246 247 248 249 250 251 252 253
                        ],
                        visualMap: {
                            min: 0,
                            max: 100,
                            inRange: {
                                color: ['white', '#0984e3'] // 控制颜色渐变的范围
                            },
                            calculable: false // 出现滑块
yma16's avatar
yma16 已提交
254
                        }
yma16's avatar
yma16 已提交
255 256
                    }
                    resolve('echarts options')
yma16's avatar
yma16 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269
                })
            }
            async function allTasks () {
                await task1() // 获取数据geojson
                await task2() // option
            }
            allTasks().then(function () {
                // console.log('热力值', hotValue)
                console.log('series', that.option.series)
                that.initMapGis(that.option)
            })
        },
        initMapGis: function (optionParams) {
yma16's avatar
yma16 已提交
270
            const that = this
yma16's avatar
yma16 已提交
271 272 273 274
            try {
                // 基于准备好的dom,初始化echarts实例
                console.log('echarts_map渲染')
                // console.log("测试data_echart",this.data,that.weather_title,this.weather_low);
yma16's avatar
yma16 已提交
275

yma16's avatar
yma16 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
                let mapEchart = that.mapEchart
                // 显示加载
                // mapEchart.showLoading()
                console.log('配置之前', optionParams)
                mapEchart.setOption(optionParams)
                // 关闭加载
                mapEchart.hideLoading()
                // 重绘
                window.onresize = function () {
                    mapEchart.resize()
                }
                // 点击事件传递参数
                mapEchart.on('click', function (params) {
                    let name = '贵阳市'
                    if (params.name && params.name !== '') {
                        name = params.name
                    }
                    console.log('点击参数', params)
                    console.log('选择行政区', name)
                    // window.alert(name)
                    that.clickCity = name
                })
            } catch (e) {
yma16's avatar
yma16 已提交
299
                throw Error(e)
yma16's avatar
yma16 已提交
300 301 302 303
            }
        }
    }
}
yma16's avatar
yma16 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
</script>

<style scoped>
.container {
  position: relative;
  width: 100%;
  height: 100%;
  /* display: flex; */
}

.mapClass {
  position: relative;
  width: 100%;
  height: 100%;
  background: #7f7fd5;
  /* fallback for old browsers */
  background: -webkit-linear-gradient(to right, #91eae4, #86a8e7, #7f7fd5);
  /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(to right, #91eae4, #86a8e7, #7f7fd5);
  /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
  /* background-size: 300%; */
}

.boxRight {
  position: fixed;
  margin: 20px;
  float: right;
  top: 5%;
  right: 0;
  width: 400px;
  height: auto;
  background: rgb(212, 190, 180);
  border-radius: 5px;
}

.refreshBtn {
  position: relative;
  left: 50%;
  transform: translateX(-50%);
yma16's avatar
yma16 已提交
343
  text-align: center;
yma16's avatar
yma16 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
  margin-top: 5px;
}

.rBtn {
  position: relative;
  width: 80px;
  height: 40px;
  left: 50%;
  transform: translateX(-50%);
  border: none;
  cursor: pointer;
  background: indianred;
  color: #fff;
  margin-top: 5px;
}
.rBtn:focus {
  background: blue;
}
.selectPoint {
  position: relative;
  margin-top: 5px;
  left: 50%;
  transform: translateX(-50%);
  width: 60%;
  overflow: hidden;
  margin-bottom: 5px;
  color: #000;
  /* box-sizing: border-box; */
  /* display: inline-block; */
}
</style>