openLayers动态绘制专题图

1 篇文章 0 订阅
1 篇文章 0 订阅

目录

数据获取

代码示例

效果展示

数据获取

数据获取来源:DataV.GeoAtlas地理小工具系列 

数据格式:GeoJSON

 

代码示例

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>OpenLayers动态绘制专题图</title>
		<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/css/ol.css">
		<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/build/ol.js"></script>
		<style type="text/css">
			#map,
			html,
			body {
				height: 100%;
				width: 100%;
			}
		</style>

	</head>
	<body>
		<div id="map">
		</div>
	</body>

	<script type="text/javascript">
		//页面
		var view = new ol.View({
			center: [112.273486, 35.719192],
			zoom: 4,
			projection: 'EPSG:4326'
		});
		//地图
		var map = new ol.Map({
			target: 'map', //指向div
			view: view
		});
		// todo wenchenhui 2023-3-1 11:30:14 添加专题图
		var addVector = function(json) {
			var time1 = new Date().getTime();
			features = new ol.format.GeoJSON().readFeatures(json);
			var vectorSource = new ol.source.Vector({
				features: features,
			});
			var vectorLayer = new ol.layer.Vector({
				source: vectorSource,
				"name": "动态绘制专题图",
				style: styleFunction
			});
			vectorLayer._name = "动态绘制专题图";
			console.log("绘制专题图耗时:" + (new Date().getTime() - time1))
			map.addLayer(vectorLayer);
		}
		var colors = ['#f26a21', '#ee8b22', '#efb964', '#f5debc', '#f4e4d1', '#ea5e51', '#6699ff', '#99bbff',
			'#ccddff', '#ffffff'
		];
		// todo wenchenhui 2023-3-1 11:30:26 配置颜色方案
		var styleFunction = function(feature) {
			// 获取数据在哪个范围
			var color = colors[Math.ceil(Math.random() * 6)];
			let stylePolygon = new ol.style.Style({
				fill: new ol.style.Fill({
					color: color,
				}),
			});
			return stylePolygon;
		};
		// todo wenchenhui 2023-3-1 11:30:39 获取GeoJson数据
		fetch('https://geo.datav.aliyun.com/areas_v3/bound/100000_full.json')
			.then((response) => response.json())
			.then((json) =>
				addVector(json)
			);
	</script>
</html>

效果展示

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值