ECharts 渲染中欧视图(中国在左,欧洲在右)

本来上传了一份 world.js 到 CSDN。但是想着 CSDN 自动涨积分的机制就干脆再写一篇博客,以帮助那些真正需要的人。(world.js 上传还在审核,需要下载文件的留言评论。)

首先你需要一份 world.json 数据。如果没有:请移步至 ECharts 常见问题 寻找下载 world.json 的方法。

中欧视图

现在网上的大多数世界地图基本全是以欧洲为中心的,不太适合我们国内开发。其实这个时候转也很方便,只需要通过 js 代码将所有的点坐标按照一定的算法进行平移即可。

中欧视图

下面是我的部分代码,大家可以参考一下,如有疑问可留言讨论。(与 -30 比较是因为这个经度纵向经过的区域少,就“格陵兰”一个,方便修复):

/**
* 初始化 Echarts
 */
initEcharts() {
	const that = this;

	this.echartsInstance = echarts.init(document.getElementById('geocanvas'));
	// 显示加载动画
	this.echartsInstance.showLoading();
	// 获取地图数据并渲染
	fetchWorldJson().then(geojson => {
		let fixPlaces = ['格陵兰', 'Greenland'];
		// 欧洲视图 转为 中欧视图(中国在左,欧洲在右)
		let features = geojson.features;
		for (let i = 0, length = features.length; i < length; ++i) {
			let feature = features[i];
			let geometry = feature.geometry;
			let coordinates = geometry.coordinates;
			if (fixPlaces.includes(feature.properties.name)) {
				continue;
			}
			if (feature.properties.cp) {
				feature.properties.cp[0] = feature.properties.cp[0] >= -30 ? feature.properties.cp[0] - 180 : feature.properties.cp[0] + 180;
			}
			if (feature.properties.center) {
				feature.properties.center[0] = feature.properties.center[0] >= -30 ? feature.properties.center[0] - 180 : feature.properties.center[0] + 180;
			}
			for (let j = 0, length = coordinates.length; j < length; ++j) {
				let coordinate = coordinates[j];
				for (let k = 0, length = coordinate.length; k < length; ++k) {
					if (coordinate[k].length > 2) {
						for (let m = 0, length = coordinate[k].length; m < length; ++m) {
							coordinate[k][m][0] = coordinate[k][m][0] >= -30 ? coordinate[k][m][0] - 180 : coordinate[k][m][0] + 180;
						}
					} else {
						coordinate[k][0] = coordinate[k][0] >= -30 ? coordinate[k][0] - 180 : coordinate[k][0] + 180;
					}
				}
			}
		}
		// 注册地图
		echarts.registerMap('WorldMap', geojson, {
			'Greenland': {
				left: 110,
				top: 60,
				width: 60
			}
		});
		this.echartsInstance.setOption(this.option);
		// 隐藏加载动画
		this.echartsInstance.hideLoading();
		// 请求初始数据
		this.mapView.toLowerCase() === 'world' ? this.getWorldGisDot() : this.getChinaGisDot();
	}).catch(err => {
		console.error("[MapEcharts.vue] 渲染地图失败!", err);
	});
}
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值