openlayers加载geoJson格式文件(并加载json中的样式)

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

openlayers加载geoJson格式文件(并加载json中的样式)

样式如下图:
在这里插入图片描述
话不多说直接上代码和注释(geojson文件可以在我的CSDN中下载)下载
分享不易:点赞支持

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<link rel="stylesheet" href="./ol/ol.css" type="text/css">
		<script src="./ol/ol.js"></script>
		<title>map</title>
		<style>
			.map {
				width: 100%;
				height: 100%;
			}

			.ol-popup {
				position: absolute;
				background-color: white;
				box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
				padding: 15px;
				border-radius: 10px;
				border: 1px solid #cccccc;
				bottom: 12px;
				left: -50px;
				min-width: 280px;
			}

			.ol-popup:after,
			.ol-popup:before {
				top: 100%;
				border: solid transparent;
				content: " ";
				height: 0;
				width: 0;
				position: absolute;
				pointer-events: none;
			}

			.ol-popup:after {
				border-top-color: white;
				border-width: 10px;
				left: 48px;
				margin-left: -10px;
			}

			.ol-popup:before {
				border-top-color: #cccccc;
				border-width: 11px;
				left: 48px;
				margin-left: -11px;
			}

			.ol-popup-closer {
				text-decoration: none;
				position: absolute;
				top: 2px;
				right: 8px;
			}

			.ol-popup-closer:after {
				content: "✖";
			}
		</style>
	</head>
	<body>
		<div id="popup" class="ol-popup">
			<a href="#" id="popup-closer" class="ol-popup-closer"></a>
			<div id="popup-content"></div>
		</div>
		<div id="map" class="map"></div>
		<script>
			/**
			 * 使用变量缓存弹窗需要的DOM对象
			 */
			var container = document.getElementById("popup");
			var content = document.getElementById("popup-content");
			var closer = document.getElementById("popup-closer");
	        /**
	        *将十六进制的颜色值转为RGB格式
	        */
			function colorRgba(sHex, alpha) {
				// 十六进制颜色值的正则表达式
				var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
				/* 16进制颜色转为RGB格式 */
				let sColor = sHex.toLowerCase()
				if (sColor && reg.test(sColor)) {
					if (sColor.length === 4) {
						var sColorNew = '#'
						for (let i = 1; i < 4; i += 1) {
							sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
						}
						sColor = sColorNew
					}
					//  处理六位的颜色值
					var sColorChange = []
					for (let i = 1; i < 7; i += 2) {
						sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)))
					}
					// return sColorChange.join(',')
					// 或
					return 'rgba(' + sColorChange.join(',') + ',' + alpha + ')'
				} else {
					return sColor
				}
			}

			var styleFunction = function(feature) {
				return new ol.style.Style({
					stroke: new ol.style.Stroke({
						color: feature.get('stroke'),//通过要素拿到具体的值
						width: 2,
						opacity: 1
					}),
					fill: new ol.style.Fill({
						color: colorRgba(feature.get('fill'), feature.get('fill-opacity')),
						///opacity: feature.get('fill-opacity')
					}),
					text: new ol.style.Text({
						text: feature.get('name'),
						font: '12px bold serif',
						fill: new ol.style.Fill({
							color: '#000'
						}),
						stroke: new ol.style.Stroke({
							color: '#fff',
							width: 2
						})
					})
				})
			};
            /**
            *创建图层渲染geoJson文件
            */
			var layers = [
				new ol.layer.Tile({
					source: new ol.source.OSM()
				}),
				new ol.layer.Vector({
					title: 'add Layer',
					source: new ol.source.Vector({
						projection: 'EPSG:4326',
						url: "./json/map.geojson", //GeoJSON的文件路径,用户可以根据需求而改变
						format: new ol.format.GeoJSON()
					}),
					style: styleFunction
				})
			];

			/* 
			 创建一个Overlay叠加层对象用作显示弹窗
			*/
			var overlay = new ol.Overlay({
				element: container,
				// autoPan: true,
				autoPanAnimation: {
					duration: 250
				}
			});

			var map = new ol.Map({
				target: 'map',
				layers: layers,
				overlays: [overlay],
				view: new ol.View({
					center: ol.proj.fromLonLat([112.9518190800, 28.2411883300]),
					zoom: 15
				})
			});

			/**
			 * 为弹窗添加一个响应关闭的函数
			 */
			closer.onclick = function() {
				overlay.setPosition(undefined);
				closer.blur();
				return false;
			};

			/**
			 * 添加单击响应函数来处理弹窗动作
			 */
			map.on("click", function(e) {
				 /* var feature = map.getFeaturesAtPixel(e.pixel);
				 console.log(feature) */
				var pixel = map.getEventPixel(e.originalEvent);
				var hit = map.hasFeatureAtPixel(pixel);
				
				 //获取到当前像素下的feature
				var feature = map.forEachFeatureAtPixel(pixel, function (feature, layer) {
					return feature;
				});
				console.log(feature)
			    var coordinate = e.coordinate;
				content.innerHTML = "<p>你点击了这里:</p><code>" + feature.values_.name + "</code>";
				overlay.setPosition(coordinate);
			});
		</script>
	</body>
</html>

  • 17
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
### 回答1: 要加载GeoJSON数据,可以使用OpenLayers的Vector图层。首先,需要创建一个Vector图层对象,然后使用OpenLayers的Format.GeoJSON类来解析GeoJSON数据,并将其添加到Vector图层。最后,将Vector图层添加到地图即可。 以下是一个示例代码: ``` var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ url: 'path/to/your/geojson/file.geojson', format: new ol.format.GeoJSON() }) }); map.addLayer(vectorLayer); ``` 其,`url`属性指定GeoJSON文件的路径,`format`属性指定使用OpenLayersGeoJSON解析器来解析数据。最后,将Vector图层添加到地图即可。 注意:在使用OpenLayers加载GeoJSON数据时,需要确保GeoJSON文件的格式正确,否则可能会导致加载失败。 ### 回答2: Openlayers是一种开源地图框架,支持多种地图底图和图层。其加载GeoJSONOpenlayers的一项重要功能。 要加载GeoJSON,首先要创建一个新图层。新图层需要绑定一个数据源,可以是本地的一个GeoJSON文件,也可以是在线的GeoJSON链接。在创建图层之后,使用Openlayers的ol.source.Vector创建一个新的矢量数据源,并将其绑定到新图层。 然后,使用Openlayers的ol.format.GeoJSON将数据转换为矢量要素,并将其添加到矢量数据源。最后,将新图层添加到Openlayers的地图。 下面是一个加载GeoJSON的代码示例: ```javascript // 创建一个新的地图对象 var map = new ol.Map({ target: 'map', // 地图显示的容器 layers: [ // 地图图层 new ol.layer.Tile({ // 街道地图 source: new ol.source.OSM() }) ], view: new ol.View({ // 地图视图 center: ol.proj.fromLonLat([119.306239, 26.080407]), // 地图心点 zoom: 14 // 地图缩放级别 }) }); // 创建一个新的矢量数据源 var vectorSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), // 数据源格式 url: '/path/to/geojson/file.geojson' // GeoJSON文件路径 }); // 创建一个新的矢量图层 var vectorLayer = new ol.layer.Vector({ source: vectorSource // 图层数据源 }); // 将图层添加到地图 map.addLayer(vectorLayer); ``` 在上面的代码,我们创建了一个地图对象,并在地图图层添加了一个OSM街道地图。然后,我们创建了一个新的矢量数据源,并将其绑定到一个新图层。最后,我们将图层添加到地图。 值得注意的是,在GeoJSON文件路径使用了绝对路径。为了方便测试,我们可以将文件放在服务器的根目录下,然后使用绝对路径访问。在实际项目,我们可能需要使用相对路径或者动态生成路径。 总之,Openlayers提供了简单易用的API,使得加载GeoJSON变得非常容易。在实际应用,我们可以将GeoJSON数据和地图交互来打造各种应用,例如展示数据统计、可视化、分析等。 ### 回答3: OpenLayers是一种用于Web GIS开发的JavaScript框架,它支持多种数据格式的加载和显示,包括GeoJSONGeoJSON是一个用于地理数据交换的标准数据格式,它可以轻松地在WebGIS应用实现地图数据的可视化。 在OpenLayers加载GeoJSON数据,需要使用ol.source.Vector类,该类是OpenLayers用于加载和显示矢量数据的核心类。下面是一个加载GeoJSON数据的简单示例: ```javascript var vectorSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), url: 'path/to/file.geojson' }); var vectorLayer = new ol.layer.Vector({ source: vectorSource }); var map = new ol.Map({ layers: [vectorLayer], target: 'map', view: new ol.View({ center: [0, 0], zoom: 2 }) }); ``` 在这个示例,我们首先创建了一个vectorSource对象,其包含format和url两个属性,分别表示GeoJSON数据的格式和数据的路径。接着,我们使用vectorSource创建一个vectorLayer对象,并将其添加到地图上。最后,我们创建一个map对象,并将vectorLayer添加到图层数组,实现了GeoJSON数据的加载和显示。 除了使用url加载GeoJSON数据外,还可以直接使用JavaScript对象或字符串加载数据。这种方式需要将数据转换为GeoJSON格式,然后使用vectorSource.addFeatures(features)方法将数据添加到vectorSource。 总之,OpenLayers提供了简单而又强大的方法来加载和显示GeoJSON数据,使得开发WebGIS应用变得更加容易和高效。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值