-
Notifications
You must be signed in to change notification settings - Fork 12
/
CMap.vue
168 lines (159 loc) · 4.39 KB
/
CMap.vue
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
<template>
<div class="cmap-container" v-bind:style="{ height: height, width: width}">
<transition name="fade">
<div class="cmap-btn" v-if="zoomed" @click="back">返回</div>
</transition>
<div class="cmap-info" v-if="hasInfo">
<b>{{currentTile}}</b>: {{currentTileVal}}
</div>
<transition name="fade">
<div class="cmap-mask" v-if="isLoading">
<p>Loading…</p>
</div>
</transition>
</div>
</template>
<script>
import 'leaflet/dist/leaflet.css'
import L from 'leaflet'
import loader from './modules/loader'
import render from './modules/render'
import initConf from './modules/conf'
var conf = {}
export default {
props: ['mapData', 'mapConf'],
created() { conf = Object.assign({}, initConf, this.mapConf) },
data() {
return {
width: this.mapConf ? (this.mapConf.width ? this.mapConf.width : '100%') : '100%',
height: this.mapConf ? (this.mapConf.height ? this.mapConf.height : '550px') : '550px',
zoomed: false,
isLoading: false,
currentTile: null,
currentProvince: null,
currentTileVal: '-',
hasInfo: false
}
},
methods: {
back() {
render.backToCountry(this, conf, this.map)
this.zoomed = false
this.currentTile = ''
this.hasInfo = false
},
showInfo(type, name) {
var area
if (type == 'country') {
area = this.mapData.find(p => p.name == name)
}
else {
area = (() => {
var province = this.mapData.find(p => p.name == this.currentProvince)
return province ? province.children.find(c => c.name == name) : null
})()
}
this.hasInfo = true
this.currentTile = name
this.currentTileVal = area ? area.value : '-'
},
hideInfo() {
this.hasInfo = false
this.currentTile = ''
this.currentTileVal = '-'
}
},
mounted() {
if (!conf.scale) {
conf.scale = loader.getColorScale(conf.colors, this.mapData)
}
var vm = this
var map = L.map(this.$el, {
attributionControl: false,
zoomControl: conf.hasZoomControl,
boxZoom: conf.boxZoom,
doubleClickZoom: conf.doubleClickZoom,
scrollWheelZoom: conf.scrollWheelZoom,
dragging: conf.dragging,
minZoom: conf.minZoom,
maxZoom: conf.maxZoom,
maxBounds: conf.countryBounds,
}).fitBounds(conf.countryBounds)
if (conf.hasTileLayer) {
L.tileLayer('//{s}.tile.osm.org/{z}/{x}/{y}.png', {
// TODO
}).addTo(map)
}
// 初始化国家地图
vm.isLoading = true
loader.load('china', data => {
var country = L.geoJson(data, {
style: feature => render.provinceStyle(feature, conf, this.mapData),
onEachFeature: (feature, layer) => {
layer.on({
mouseover: e => render.highlightFeature(vm, 'country', conf.highlightStyle, e),
mouseout: e => render.resetHighlight(vm, country, e),
// 仅需在初始加载时判断是否需要城市视图
click: conf.hasCityView ? e => render.zoomIn(vm, conf, country, map, e) : () => {}
})
}
}).addTo(map);
vm.isLoading = false
})
vm.map = map
}
}
</script>
<style scoped>
.cmap-container {
position: relative;
margin: 0 auto;
}
.cmap-info, .cmap-btn {
position: absolute;
height: 25px;
padding: 5px 15px;
font: 14px Arial, Helvetica, sans-serif;
line-height: 25px;
vertical-align: middle;
border-radius: 4px;
box-shadow: 0 2px 3px grey;
color: black;
background: white;
z-index: 999;
cursor: pointer;
}
.cmap-btn {
bottom: 15px;
left: 10px;
}
.cmap-info {
bottom: 15px;
right: 10px;
}
.cmap-mask {
position: absolute;
width: 100%;
height: 100%;
display: flex;
justify-content: space-around;
align-items: center;
background-color: rgba(100, 100, 100, .4);
z-index: 1000;
}
.cmap-mask > p {
display: block;
text-align: center;
color: white;
font: 20px Arial, Helvetica, sans-serif;
}
.leaflet-container {
background: transparent !important;
}
.fade-enter-active, .fade-leave-active {
transition: opacity .4s ease;
}
.fade-enter, .fade-leave-active {
opacity: 0
}
</style>