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>
相关推荐
2501_915106321 小时前
iOS 26 APP 性能测试实战攻略:多工具组合辅助方案
android·macos·ios·小程序·uni-app·cocoa·iphone
巨神峰SEO4 小时前
支付宝小程序 SEO 实战:鲜花送达类小程序抢占搜索流量指南
小程序·支付宝小程序排名优化·支付宝小程序seo·支付宝小程序排名·支付宝优化
凉辰5 小时前
微信小程序uni.request 返回值存在精度丢失问题
微信小程序·小程序
李慕婉学姐6 小时前
【开题答辩过程】以《基于微信小程序教学评价平台的设计与实现》为例,不会开题答辩的可以进来看看
微信小程序·小程序
计算机学姐6 小时前
基于微信小程序的垃圾分类管理系统【2026最新】
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
weixin_lynhgworld9 小时前
剧本杀小程序系统开发:内容生态与商业模式的双轮驱动
大数据·小程序
博客zhu虎康9 小时前
微信小程序报错 ubepected character `的style换行问题
微信小程序
计算机徐师兄9 小时前
Java基于SpringBoot的农场管理系统小程序【附源码、文档说明】
java·微信小程序·小程序·农场管理系统小程序·java农场管理系统小程序·java农场管理系统微信小程序·农场管理微信小程序
00后程序员张10 小时前
iOS混淆与IPA文件加固全流程实战 防止苹果应用被反编译的工程级方案
android·ios·小程序·https·uni-app·iphone·webview
草字10 小时前
uniapp 打开横竖屏。usb调试时可以横竖屏切换,但是打包发布后却不行?
java·前端·uni-app