【问题标题】:How to get the coordinates of a drawn box in OpenLayers?如何在 OpenLayers 中获取绘制框的坐标?
【发布时间】:2016-08-02 06:16:36
【问题描述】:

我是 OpenLayers 的新手,很抱歉有一个明显的(也许是愚蠢的)问题,我找到了不同的解决方案方法,但没有一个有效。尝试了这个和那个,十几个不同的建议(hereherehereherehere)但都是徒劳的。

基本上,我想将绘制的矩形的坐标传递给另一个 Web 服务。因此,在绘制完矩形后,它应该会吐出边界框的四个角。

到目前为止,我所拥有的是用于绘制矩形的基本 OL 图层示例:

    var source = new ol.source.Vector({wrapX: false});


    vector = new ol.layer.Vector({
        source: source,
        style: new ol.style.Style({
            fill: new ol.style.Fill({
                color: 'rgba(0, 255, 0, 0.5)'
            }),
            stroke: new ol.style.Stroke({
                color: '#ffcc33',
                width: 2
            }),
            image: new ol.style.Circle({
                radius: 7,
                fill: new ol.style.Fill({
                color: '#ffcc33'
                })
            })
        })      
    });



    var map = new ol.Map({
        target: 'map',
        layers: [
            new ol.layer.Tile({
                source: new ol.source.OSM()
            }),
            vector
        ],
        view: new ol.View({
            center: ol.proj.fromLonLat([37.41, 8.82]),
            zoom: 4
        })
    });



    var draw; // global so we can remove it later
    function addInteraction() 
    {
        var value = 'Box';

        if (value !== 'None') 
        {
            var geometryFunction, maxPoints;
            if (value === 'Square') 
            {
                value = 'Circle';
                geometryFunction = ol.interaction.Draw.createRegularPolygon(4);
            } 
            else if (value === 'Box') 
            {
                value = 'LineString';
                maxPoints = 2;
                geometryFunction = function(coordinates, geometry)
                {
                    if (!geometry) 
                    {
                        geometry = new ol.geom.Polygon(null);
                    }
                    var start = coordinates[0];
                    var end = coordinates[1];
                    geometry.setCoordinates([
                    [start, [start[0], end[1]], end, [end[0], start[1]], start]
                    ]);
                    return geometry;
                };
            }

            draw = new ol.interaction.Draw({
                source: source,
                type: /** @type {ol.geom.GeometryType} */ (value),
                geometryFunction: geometryFunction,
                maxPoints: maxPoints            
            });

            map.addInteraction(draw);               
        }
    }


    addInteraction();           

现在,接下来会发生什么?有什么好的提取边界框的方法?

感谢任何提示!

【问题讨论】:

    标签: coordinates openlayers-3 rectangles


    【解决方案1】:

    您需要为绘图交互指定一个侦听器。像这样:

    draw.on('drawend',function(e){
    alert(e.feature.getGeometry().getExtent());
    });
    

    这是fiddle

    【讨论】:

    • 啊太棒了!非常感谢。然而有趣的是,小提琴在 Safari 中确实有效,但在我这边却没有实现——在另一边,它在 Firefox 和 Chrome(所有 Mac)中都有效......
    • 这是一个官方的ol3示例(openlayers.org/en/latest/examples/draw-features.html),我刚刚添加了监听器。确实很奇怪,它不适用于野生动物园。我没有野生动物园,所以我无法测试它。很高兴为您提供帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    相关资源
    最近更新 更多