map.vue
5.5 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<template>
<view class="qiun-columns">
<view class="qiun-charts">
<!--#ifdef MP-ALIPAY -->
<canvas canvas-id="canvasMap" id="canvasMap" class="charts" :width="cWidth*pixelRatio" :height="cHeight*pixelRatio"
:style="{'width':cWidth+'px','height':cHeight+'px'}" @touchstart="touchMap"></canvas>
<!--#endif-->
<!--#ifndef MP-ALIPAY -->
<canvas canvas-id="canvasMap" id="canvasMap" class="charts" @touchstart="touchMap"></canvas>
<!--#endif-->
<view class="fugai">覆盖地区</view>
<view class="dian">点亮中国</view>
</view>
</view>
</template>
<script>
import app from "../../App.vue";
import uCharts from '@/components/u-charts/u-charts.js';
import {
isJSON
} from '@/common/checker.js';
var _self;
var canvaMap = null;
export default {
data() {
return {
cWidth: '',
cHeight: '',
pixelRatio: 1,
textarea: '',
cityname: ''
}
},
onLoad() {
_self = this;
//#ifdef MP-ALIPAY
uni.getSystemInfo({
success: function(res) {
if (res.pixelRatio > 1) {
//正常这里给2就行,如果pixelRatio=3性能会降低一点
//_self.pixelRatio =res.pixelRatio;
_self.pixelRatio = 3;
}
}
});
//#endif
this.cWidth = uni.upx2px(750);
this.cHeight = uni.upx2px(750);
// 获取覆盖城市
this.getcity()
},
methods: {
getServerData() {
let that=this;
uni.showLoading({
title: '加载中'
})
// https://geo.datav.aliyun.com/areas/bound/100000_full.json
// https://geo.datav.aliyun.com/areas_v2/bound/100000_full.json
// https://geo.datav.aliyun.com/areas_v2/bound/100000.json
uni.request({
url: ' https://geo.datav.aliyun.com/areas/bound/100000_full.json',
data: {},
success: function(res) {
console.log(res);
let data = res.data.features;
data.forEach(function(value, index, array) {
value.color = "#CBE2E6";
if (value.properties.name == "新疆维吾尔自治区") {
value.properties.name = "新疆"
} else if (value.properties.name == '内蒙古自治区') {
value.properties.name = "内蒙古"
} else if (value.properties.name == '天津市') {
value.properties.name = "天津"
} else if (value.properties.name == '北京市') {
value.properties.name = "北京"
} else if (value.properties.name == '上海市') {
value.properties.name = "上海"
} else if (value.properties.name == '西藏自治区') {
value.properties.name = "西藏"
} else if (value.properties.name == '广西壮族自治区') {
value.properties.name = "广西"
} else if (value.properties.name == '香港特别行政区') {
value.properties.name = "香港"
} else if (value.properties.name == '澳门特别行政区') {
value.properties.name = "澳门"
} else if (value.properties.name == '宁夏回族自治区') {
value.properties.name = '宁夏'
}else if (value.properties.name == '重庆市') {
value.properties.name = '重庆'
}else {
let name = value.properties.name.split('省');
console.log('3277344',name)
value.properties.name = name[0];
}
console.log(value.properties.name,9999)
})
uni.hideLoading()
let cMap = {
series: []
};
//这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去
// ssssconsole.log(newmap.features)
data.forEach(function(value, index, array) {
that.cityname.forEach(function(valuek, indexk, arrayk) {
if (value.properties.name == valuek.shortname) {
console.log(value.properties.name)
value.color = "#F5F208";
}
})
})
cMap.series = data
_self.showMap("canvasMap", cMap);
},
fail: () => {
_self.tips = "网络错误,小程序端请检查合法域名";
},
});
},
showMap(canvasId, chartData) {
canvaMap = new uCharts({
$this: _self,
canvasId: canvasId,
type: 'map',
fontSize: 7,
padding: [0, 0, 0, 0],
legend: {
show: false
},
background: '#FFFFFF',
pixelRatio: _self.pixelRatio,
series: chartData.series,
dataLabel: true,
width: _self.cWidth * _self.pixelRatio,
height: _self.cHeight * _self.pixelRatio,
extra: {
map: {
border: true,
borderWidth: 1,
borderColor: '#C29D5A',
fillOpacity: 0.6
}
}
});
},
touchMap(e) {
console.log(e)
canvaMap.showToolTip(e, {
format: function(item) {
console.log(item)
return item.properties.name
}
});
},
// 获取覆盖城市
getcity() {
let that = this;
var url = 'sundry/get_area_province';
var params = {
}
console.log('7766554', params)
app.post(url, params).then((res) => {
console.log(res)
that.cityname = res.data.data;
console.log(that.cityname)
this.getServerData();
}).catch((err) => {
console.log(err,999888)
})
},
}
}
</script>
<style>
/*样式的width和height一定要与定义的cWidth和cHeight相对应*/
.qiun-charts {
width: 750upx;
height: 750upx;
background-color: #FFFFFF;
}
.charts {
width: 750upx;
height: 750upx;
background-color: #FFFFFF;
}
.dian{
color:#3D454C;
font-size: 64rpx;
font-weight: bold;
text-align: center;
margin: 100rpx auto 0;;
}
.fugai{
color:#3D454C;
font-size: 32rpx;
text-align: center;
margin: 100rpx auto 0;
}
</style>