openLayers动态绘制专题图

1 篇文章 0 订阅
1 篇文章 0 订阅
该文章展示了如何利用OpenLayers库,结合GeoJSON数据格式,动态绘制地图专题图。代码示例中,首先设置了地图视图,然后加载GeoJSON数据,创建并应用自定义样式函数以改变区域颜色,最后将专题图层添加到地图上。
摘要由CSDN通过智能技术生成

目录

数据获取

代码示例

效果展示

数据获取

数据获取来源: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
    评论
OpenLayers是一个开源的JavaScript库,用于创建交互式的Web地。在OpenLayers中,动态绘制矩形可以通过事件监听和坐标处理来实现。下面是简单的步骤: 1. 引入OpenLayers库和相关的模块: ```html <script src="https://openlayers.org/en/v6.6.3/build/ol.js"></script> ``` 2. 创建一个`Map`对象,并添加一个`View`: ```javascript var map = new ol.Map({ target: 'map', // 在HTML中指定容器id view: new ol.View({ center: [0, 0], // 地中心点 zoom: 2 // 初始缩放级别 }) }); ``` 3. 添加层(如WMS、Tiles或GeoJSON): ```javascript var layer = new ol.layer.Tile({source: new ol.source.OSM()}); map.addLayer(layer); ``` 4. 使用鼠标事件监听器来绘制矩形: ```javascript map.on('pointerdown', function(e) { var rectangle = new ol.geom.Rectangle([e.coordinate]); // 基于点击坐标创建初始矩形 var drawnFeature = new ol.Feature(rectangle); var vectorSource = new ol.source.Vector({features: [drawnFeature]}); var vectorLayer = new ol.layer.Vector({source: vectorSource}); map.addLayer(vectorLayer); map.forEachFeatureAtPixel(e.pixel, function(feature) { if (feature instanceof ol.Feature) { feature.setGeometry(null); // 清除原有矩形 drawnFeature.setGeometry(rectangle.extend(feature.getGeometry().getExtent())); // 更新矩形大小到包括鼠标下的所有特征 } }); }); // 当鼠标抬起时,清除矩形 map.on('pointerup', function() { map.forEachFeatureAtPixel(map.getEventPixel(e), function(feature) { if (feature instanceof ol.Feature) { feature.setGeometry(null); } }); }); ``` 这里我们监听了`pointerdown`事件开始绘制矩形,`pointerup`事件结束绘制并清除矩形。注意这只是一个基本示例,实际应用可能还需要处理移动、拖拽等更复杂的交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值