OpenLayers基础教程——要素的编辑

1、前言

OpenLayers中,要素的编辑主要使用ol.interaction.Selectol.interaction.Modify配合实现,下面开始介绍。

2、编辑要素

编辑功能的实现很简答,ol.interaction.Select负责选择要素,ol.interaction.Modify对被选择的要素进行编辑,代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>编辑要素</title>
    <link href="lib/ol/ol.css" rel="stylesheet" />
    <script src="lib/ol/ol.js"></script>
</head>
<body>
    <div id="map" style="width: 800px; height: 800px;"></div>
    <script>
        // 创建图层
        var layer = new ol.layer.Vector({
            source: new ol.source.Vector({
                features: [
                    new ol.Feature({
                        geometry: new ol.geom.Point([119.4, 29.6]),
                    }),
                    new ol.Feature({
                        geometry: new ol.geom.LineString([
                            [119.0, 29.4],
                            [119.2, 29.3],
                            [119.4, 29.5]
                        ])
                    }),
                    new ol.Feature({
                        geometry: new ol.geom.Polygon([[
                            [119.4, 29.0],
                            [119.6, 29.0],
                            [119.5, 29.2],
                            [119.4, 29.0]
                        ]])
                    })
                ]
            }),
            style: new ol.style.Style({
                image: new ol.style.Circle({
                    radius: 30,
                    fill: new ol.style.Fill({
                        color: 'red'
                    })
                }),
                stroke: new ol.style.Stroke({
                    color: 'blue',
                    width: 2
                }),
                fill: new ol.style.Fill({
                    color: 'yellow'
                })
            })
        });

        // 创建地图
        var map = new ol.Map({
            target: 'map',
            layers: [
                layer
            ],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [119.2, 29.2],
                zoom: 10
            })
        });

        // 创建选择工具
        var select = new ol.interaction.Select({
            condition: ol.events.condition.singleClick,
            style: new ol.style.Style({
                image: new ol.style.Circle({
                    radius: 30,
                    fill: new ol.style.Fill({
                        color: 'red'
                    })
                }),
                stroke: new ol.style.Stroke({
                    color: 'blue',
                    width: 2
                }),
                fill: new ol.style.Fill({
                    color: 'yellow'
                })
            }),
            layers: [
                layer
            ]
        });

        // 创建修改工具
        var modify = new ol.interaction.Modify({
            features: select.getFeatures()
        });

        // 添加交互工具
        map.addInteraction(select);
        map.addInteraction(modify);
    </script>
</body>
</html>

运行结果如下图所示:

在这里插入图片描述

3、要素编辑后的处理

在对要素进行拖动或者拉伸等操作后,其坐标肯定会随之发生改变,如果要获取编辑后的要素坐标,则需要监听modifyend事件,代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>编辑要素</title>
    <link href="lib/ol/ol.css" rel="stylesheet" />
    <script src="lib/ol/ol.js"></script>
</head>
<body>
    <div id="map" style="width: 800px; height: 800px;"></div>
    <script>
        // 创建图层
        var layer = new ol.layer.Vector({
            source: new ol.source.Vector({
                features: [
                    new ol.Feature({
                        geometry: new ol.geom.Point([119.4, 29.6]),
                    }),
                    new ol.Feature({
                        geometry: new ol.geom.LineString([
                            [119.0, 29.4],
                            [119.2, 29.3],
                            [119.4, 29.5]
                        ])
                    }),
                    new ol.Feature({
                        geometry: new ol.geom.Polygon([[
                            [119.4, 29.0],
                            [119.6, 29.0],
                            [119.5, 29.2],
                            [119.4, 29.0]
                        ]])
                    })
                ]
            }),
            style: new ol.style.Style({
                image: new ol.style.Circle({
                    radius: 30,
                    fill: new ol.style.Fill({
                        color: 'red'
                    })
                }),
                stroke: new ol.style.Stroke({
                    color: 'blue',
                    width: 2
                }),
                fill: new ol.style.Fill({
                    color: 'yellow'
                })
            })
        });

        // 创建地图
        var map = new ol.Map({
            target: 'map',
            layers: [
                layer
            ],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [119.2, 29.2],
                zoom: 10
            })
        });

        // 创建选择工具
        var select = new ol.interaction.Select({
            condition: ol.events.condition.singleClick,
            style: new ol.style.Style({
                image: new ol.style.Circle({
                    radius: 30,
                    fill: new ol.style.Fill({
                        color: 'red'
                    })
                }),
                stroke: new ol.style.Stroke({
                    color: 'blue',
                    width: 2
                }),
                fill: new ol.style.Fill({
                    color: 'yellow'
                })
            }),
            layers: [
                layer
            ]
        });

        // 创建修改工具
        var modify = new ol.interaction.Modify({
            features: select.getFeatures()
        });

        // 监听modifyend事件,获取要素修改后的坐标
        modify.on('modifyend', function (e) {
            var features = e.features;
            if (features.getLength() > 0) {
                var feature = features.item(0);
                var geometry = feature.getGeometry();
                var coordinates = geometry.getCoordinates();

                // 点
                if (geometry instanceof ol.geom.Point) {
                    console.log('更新后的点坐标:[' + coordinates + ']');
                }

                // 线
                if (geometry instanceof ol.geom.LineString) {
                    var msg = [];
                    for (var i = 0; i < coordinates.length; i++) {
                        msg.push('[' + coordinates[i] + ']');
                    }
                    console.log('更新后的线坐标:' + msg.join('、'));
                    msg = [];
                }

                // 面
                if (geometry instanceof ol.geom.Polygon) {
                    var msg = [];
                    for (var i = 0; i < coordinates.length; i++) {
                        var coordinate = coordinates[i];
                        for (var j = 0; j < coordinate.length; j++) {
                            msg.push('[' + coordinate[j] + ']');
                        }
                    }
                    console.log('更新后的面坐标:' + msg.join('、'));
                    msg = [];
                }
            }
        });

        // 添加交互工具
        map.addInteraction(select);
        map.addInteraction(modify);
    </script>
</body>
</html>

运行结果如下图所示:
在这里插入图片描述

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值