uni-app 微信小程序之加载行政区图

文章目录

  • [1. 实现效果](#1. 实现效果)
  • [2. 实现步骤](#2. 实现步骤)

1. 实现效果

2. 实现步骤

  1. 使用三方组件 ucharts echarts 高性能跨全端图表组件
  2. 页面导入引入的三方组件
  3. 组件demo代码
html 复制代码
<template>
	<view class="qiun-columns">
		<view class="cu-bar bg-white margin-top-xs">
			<view class="action sub-title">
				<text class="text-xl text-bold text-blue text-shadow">行政区域图</text>
				<text class="text-ABC text-blue">District</text>
			</view>
		</view>

		<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>
	</view>
</template>

<script>
	import uCharts from '@/components/u-charts/u-charts.js';
	import {
		isJSON
	} from '@/components/u-charts/checker.js';
	var _self;
	var canvaMap = null;

	export default {
		data() {
			return {
				city: '',
				cWidth: '',
				cHeight: '',
				pixelRatio: 1,
				textarea: '',
			}
		},
		onLoad() {
			_self = this;
			//#ifdef MP-ALIPAY
			uni.getSystemInfo({
				success: function(res) {
					if (res.pixelRatio > 1) {
						//正常这里给2就行,如果pixelRatio=3性能会降低一点
						//_self.pixelRatio =res.pixelRatio;
						_self.pixelRatio = 2;
					}
				}
			});
			//#endif
			this.cWidth = uni.upx2px(750);
			this.cHeight = uni.upx2px(750);
			this.getServerData();
		},
		methods: {
			getServerData() {
				uni.request({
					url: 'https://cdn.xxx.com/dataV.json',//替换为你组件的组件行政区域
					data: {},
					success: function(res) {
						let cMap = {
							series: []
						};
						cMap.series =res.data.features;
						console.log(res.data)
						_self.showMap("canvasMap", cMap);
					},
					fail: () => {
						_self.tips = "网络错误,小程序端请检查合法域名";
					},
				});
			},
			showMap(canvasId, chartData) {
				canvaMap = new uCharts({
					$this: _self,
					canvasId: canvasId,
					type: 'map',
					fontSize: 11,
					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: '#666666',
							fillOpacity: 0.6
						}
					}
				});
			},
			touchMap(e) {
				canvaMap.showToolTip(e, {
					format: function(item) {
						console.log(item.properties)
						_self.city = item.properties.name;
						return `这里是:${item.properties.name}`
					}
				});
			}
		}
	}
</script>

<style>
	/*样式的width和height一定要与定义的cWidth和cHeight相对应*/
	.qiun-charts {
		width: 750upx;
		height: 750upx;
		background-color: #FFFFFF;

	}

	.charts {
		width: 750upx;
		height: 750upx;
		padding: 30rpx 0;
		background-color: #FFFFFF;
	}
</style>
相关推荐
丁总学Java10 分钟前
微信小程序-npm支持-如何使用npm包
前端·微信小程序·npm·node.js
工业互联网专业1 小时前
毕业设计选题:基于ssm+vue+uniapp的校园水电费管理小程序
vue.js·小程序·uni-app·毕业设计·ssm·源码·课程设计
说私域5 小时前
社群团购中的用户黏性价值:以开源小程序多商户AI智能名片商城源码为例
人工智能·小程序
以对_9 小时前
uview表单校验不生效问题
前端·uni-app
迷雾yx9 小时前
开发微信小程序 基础02
微信小程序·小程序
迷雾yx9 小时前
开发微信小程序 基础03
微信小程序·小程序
说私域9 小时前
地理定位营销与开源AI智能名片O2O商城小程序的融合与发展
人工智能·小程序
小雨cc5566ru20 小时前
uniapp+Android面向网络学习的时间管理工具软件 微信小程序
android·微信小程序·uni-app
二十雨辰1 天前
[uni-app]小兔鲜-04推荐+分类+详情
前端·javascript·uni-app
小雨cc5566ru1 天前
hbuilderx+uniapp+Android健身房管理系统 微信小程序z488g
android·微信小程序·uni-app