openlayers划线、标记点

参考代码1:https://blog.csdn.net/zy13608089849/article/details/70815887
参考代码2:http://t.csdn.cn/35iol

实现结果:

  • 只有线段:在这里插入图片描述
  • 只有标记点:
    在这里插入图片描述
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="./ol/ol.css">
    <style>
        .map-class {
            height: 100rem;
            width: 100%;
        }
    </style>
    <!-- 加载OpenLayers 类库 -->
    <script src="./ol/ol.js"></script>
    <title>天地图划线</title>
</head>

<body>
    <!-- <img src="./img/arrow.png"> -->
    <div id="map-id" class="map-class"></div>
    <script type="text/javascript">
        var center = [103, 45] //中心经纬度
        var view = new ol.View({
            center: ol.proj.fromLonLat(center),  //默认使用EPSG:3857地图坐标系,引入输出一般使用EPSG:4326实际坐标系,需要转换
            zoom: 5,
            minZoom: 1,
            maxZoom: 17
        })

        //新建map容器
        var map = new ol.Map({

            target: 'map-id',  // 绑定 DIV 元素
            // 添加图层
            layers: [
                // 设置图层的数据源
                //   矢量底图
                new ol.layer.Tile({
                    source: new ol.source.XYZ({
                        url: 'http://t0.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk='
                    })
                }),
                //   矢量标记
                new ol.layer.Tile({
                    source: new ol.source.XYZ({
                        url: 'http://t0.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk='
                    })
                }),
            ],
            // 设置视图窗口
            view: view
        });
        //划线准备:
        //准备新图层用来加入标注的点和线,标记数据集
        var source = new ol.source.Vector();

        //标记层
        var layer = new ol.layer.Vector({
            source: new ol.source.Vector()
        });
        //定义标注点坐标
        //散列点数组
        var coordinate = new Array();

        //散列点
        var coordinate1 = [104.1005229950, 30.6743128087];
        var coordinate2 = [103.9271879196, 30.7462617980];
        var coordinate3 = [103.6227035522, 30.9932085864];
        var coordinate4 = [103.5752069950, 31.4663367378];
        var coordinate5 = [103.4307861328, 30.1941019446];
        var coordinate6 = [106.4500522614, 29.5811456252];
        var coordinate7 = [106.5885615349, 29.5679608922];
        var coordinate8 = [107.7666950226, 29.4161988273];
        var coordinate9 = [118.1862562895, 24.4891501310];
        var coordinate10 = [118.1982564926, 24.4947641146];

        coordinate.push(coordinate1);
        coordinate.push(coordinate2);
        coordinate.push(coordinate3);
        coordinate.push(coordinate4);
        coordinate.push(coordinate5);
        coordinate.push(coordinate6);
        coordinate.push(coordinate7);
        coordinate.push(coordinate8);
        coordinate.push(coordinate9);
        coordinate.push(coordinate10);

        var geometry = new ol.geom.LineString([
            ol.proj.transform([114,27], 'EPSG:4326', 'EPSG:3857'),
            ol.proj.transform([125.151421, 42.920414], 'EPSG:4326', 'EPSG:3857'),
        ]);
        for (var i = 0; i < coordinate.length; i++) {
            // 将点加入geometry中存储几何信息;坐标系转换,保证map中坐标系一致
            geometry.appendCoordinate(ol.proj.transform(coordinate[i], 'EPSG:4326', 'EPSG:3857'));
            // 创建一个Feature,并设置好在地图上的位置
            var anchor = new ol.Feature({
                geometry: new ol.geom.Point(ol.proj.transform(coordinate[i], 'EPSG:4326', 'EPSG:3857'))
            });
            // 设置样式,在样式中就可以设置图标
            anchor.setStyle(new ol.style.Style({
                image: new ol.style.Circle({
                    radius: 10,
                    stroke: new ol.style.Stroke({
                        color: '#fff'
                    }),
                    fill: new ol.style.Fill({
                        color: '#3399CC'
                    })
                })
            }));
            // 添加到之前的创建的layer中去
            layer.getSource().addFeature(anchor);
        }

        var feature = new ol.Feature({
            geometry: geometry
        });
        source.addFeature(feature);
        //标记点集
        var vector = new ol.layer.Vector({
            source: source,
            style: styleFunction
        });

        //获取样式
        var styleFunction = function (feature) {
            var geometry = feature.getGeometry();
            //线段样式
            var styles = [
                new ol.style.Style({
                    fill: new ol.style.Fill({
                        color: '#0044CC'
                    }),
                    stroke: new ol.style.Stroke({
                        lineDash: [1, 3, 5],
                        width: 2,
                        color: [255, 0, 0, 1]
                    })
                })
            ];
            //箭头样式
            geometry.forEachSegment(function (start, end) {
                var arrowLonLat = [(end[0] + start[0]) / 2, (end[1] + start[1]) / 2];
                var dx = end[0] - start[0];
                var dy = end[1] - start[1];
                var rotation = Math.atan2(dy, dx);
                styles.push(new ol.style.Style({
                    geometry: new ol.geom.Point(arrowLonLat),
                    image: new ol.style.Icon({
                        src: "./image/arrow.png",
                        anchor: [0.75, 0.5],
                        rotateWithView: true,
                        rotation: -rotation
                    })
                }));
            });
            return styles;
        };

        // map.addLayer(vector) // 线段
        map.addLayer(layer)  // 标记点


    </script>
</body>

</html>

划线流程

  1. 定义轨迹路线:new ol.geom.LineString
    var geometry = new ol.geom.LineString([], 'XYZM');
    
  2. 给轨迹定义相应样式:
       var styles = [
               new ol.style.Style({
                   fill: new ol.style.Fill({
                       color: '#0044CC'
                   }),
                   stroke: new ol.style.Stroke({
                       lineDash: [1, 3, 5],
                       width: 2,
                       color: [255, 0, 0, 1]
                   })
               })
           ];
    
  3. 新建矢量图层
    layer = new ol.layer.Vector({
        source: new ol.source.Vector({
          features: [gemoetry,...,...],
        }), 
        style: styleFunction;
      });
    
  4. 将矢量图层添加到map中
    map.addLayer(layer)
    

或者在地图初始化中加入:

map = new ol.Map({
	target:
	layers:[layer,....]
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值