openlayers 画线、修改线并获取线的坐标组(console,log() 输出)

复制后 需要 ol.js  还有 jquery.js

<html>
<head>
<meta charset="UTF-8">
<title>测试页</title>
<!-- 图层样式 -->
<link rel="stylesheet" type="text/css" href="https://openlayers.org/en/v5.3.0/css/ol.css" />
<script src="ol/ol.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery-1.11.3.min.js" type="text/javascript" charset="utf-8"></script>

</head>
<body >
	
<div class="form-group" id="thumbnailUploadContainer" class="col-sm-10" style="float: left; margin-right: 50px;">
	<div id="map" class="map" style="position: absolute; width: 100%; height: 100%;"></div>
	   <div id="" style="position: absolute;left: 10px;top: 20px;float: left;margin-left: 10px;">
	        <button type="button" id="okDraw">开始画线</button>
	        <button type="button" id="noDraw" >停止画线</button>
	        <button type="button" id="okSelect" >开始修改</button>
	        <button type="button" id="noSelect" >停止修改</button>
	    </div>
</div>
</body>
<script>
$('body').on('change','#articleImageFile',function(){
    var formData = new FormData();
    var files = $($(this))[0].files[0];
    var name = $($(this)).val();
    console.log(files);    
    formData.append("file", files);
    //另外加的参数
    //另外加的参数
    $.ajax({
        url: 'excel/import.do',
        type: 'POST',
        data: formData,
        processData: false,
        contentType: false, 
        dataType: 'json',
        success:function (data) {
            alert('上传成功');
        },
        error:function (data) {
        	 console.log('302');
        	// window.history.go(0);
            	window.history.go(0);
            //	alert('表导入成功!');
            	console.log("jingru 方法");
        }
    });
})
	var tian_di_tu_road_layer = new ol.layer.Tile({
		title: "天地图路网",
        source: new ol.source.XYZ({
            url: "http://t4.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=49ea1deec0ffd88ef13a3f69987e9a63"
        })
	});
	
	//画线的矢量层
	      var source = new ol.source.Vector();
	      var styleFunction = function(feature) {
	        var geometry = feature.getGeometry();
	        var styles = [
	          new ol.style.Style({
	            stroke: new ol.style.Stroke({
	               color: '#ffcc33',
	              width: 2
	            })
	          })
	        ];
	        return styles;
	      };
	      var vector = new ol.layer.Vector({
	        source: source,
	        style: styleFunction//画成功后 线的默认样式
	      });
	
	//声明map
			var map = new ol.Map({
				layers: [
					tian_di_tu_road_layer,
					vector
				],
				target: 'map',
				view: new ol.View({
					center: [13279385.112932026, 5878884.034557744],
					zoom: 6
				})
			});
	var drawLine = new ol.interaction.Draw({
	        source: source,
	        type: 'LineString'
	      })
	    drawLine.on("drawstart",function (e) {
	        console.log("!!!!我要开始画线了!!!!");
	    });
	    drawLine.on("drawend",function (e) {
	        console.log("!!!!我画完线了!!!!");
	        f = e.feature;
	        console.log(coordConvert(f));
	        map.removeInteraction(drawLine);
	    });
	    var select=new ol.interaction.Select({wrapX: false}); 
	        var selectFeature;
	        // map.addInteraction(select);
	        select.on("select",function (e) {
	            f=e.selected[0];
	                var modify = new ol.interaction.Modify({features: select.getFeatures()});
	                map.addInteraction(modify);
	                modify.on("modifyend",function (e) {
	                    var f = e.features.getArray()[0];
	                    console.log("这个应该是修改后的坐标信息:"+coordConvert(f));
	                });    
	        });
	            function coordConvert(f){
	                lineFeature = f.getGeometry().clone();
	                lineFeature.applyTransform(ol.proj.getTransform('EPSG:3857', 'EPSG:4326'));
	                lineFeature = new ol.Feature(lineFeature);
	                var coordStr = lineFeature.getGeometry().getCoordinates().toString();
	                console.log(coordStr);
	                var coordArr = coordStr.split(",");
	                var coordinateStr = new Array();
	                coordinateStr.push("[[");
	                for(var i in coordArr) {
	                    if(i==0){
	                        coordinateStr.push(coordArr[i]);
	                    }else{
	                        if(i%2==0){
	                            coordinateStr.push("],["+coordArr[i]);
	                        }else if(i%2==1){
	                            coordinateStr.push(","+coordArr[i]);
	                        }
	                    }
	                };
	                coordinateStr.push("]]");
	                return coordinateStr.join("");
	            }
	     function okDraw(){
	         map.addInteraction(drawLine);//可以画线
	        map.removeInteraction(select);
	     }
	     function noDraw(){
	         map.removeInteraction(drawLine); //停止画线
	     }
	     function okSelect(){
	         map.addInteraction(select);//允许修改矢量信息
	        map.removeInteraction(drawLine);
	     }
	     function noSelect(){
	          map.removeInteraction(select);//禁止修改矢量信息
	     }
		document.getElementById("okDraw").addEventListener("click", function() {
			okDraw();
		}, true);
		document.getElementById("noDraw").addEventListener("click", function() {
			noDraw();
		}, true);
		document.getElementById("okSelect").addEventListener("click", function() {
			okSelect();
		}, true);
		document.getElementById("noSelect").addEventListener("click", function() {
			noSelect();
		}, true);

</script>
</html>

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
OpenLayers是一个强大的开源JavaScript库,用于构建交互式的Web地图应用。要使用OpenLayers在地图上画一条线(Polyline),你可以按照以下步骤操作: 1. 引入OpenLayers库:首先在HTML文件中引入OpenLayers的CDN链接,或者本地包含其文件。 ```html <script src="https://cdn.jsdelivr.net/npm/openlayers@6.6.1/dist/OpenLayers.js"></script> ``` 2. 初始化地图:创建一个`<div>`容器,并设置地图的视口大小。 ```html <div id="map" style="width: 100%; height: 500px;"></div> ``` 3. 创建地图实例:在JavaScript中,初始化地图并设置投影和分辨率。 ```javascript const map = new ol.Map({ target: 'map', view: new ol.View({ center: [0, 0], // 设置中心点 zoom: 2, // 设置初始缩放级别 projection: 'EPSG:4326', // 使用WGS84坐标系 }), }); ``` 4. 添加在线层或地形数据:通常我们会添加一个WMS或TMS服务作为底图。 ```javascript const layer = new ol.layer.Tile({ source: new ol.source.OSM(), // 或者使用其他在线地图服务 }); map.addLayer(layer); ``` 5. 创建线条矢量层:使用`ol.format.GeoJSON`将线条数据转换为OpenLayers可以理解的格式。 ```javascript const lineFeature = { type: 'Feature', geometry: { type: 'LineString', coordinates: [[0, 0], [10, 10]], // 线条起点和终点坐标 }, }; const lineSource = new ol.source.Vector({ url: 'data/lines.geojson', // 如果是本地文件,路径改为本地 format: new ol.format.GeoJSON(), features: [ol.format.GeoJSON.writeFeature(lineFeature)], }); const lineLayer = new ol.layer.Vector({ source: lineSource, style: new ol.style.Stroke({ color: 'blue', width: 2, }), }); map.addLayer(lineLayer); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值