openlayers4获取一个图形的边界

由于项目需求问题,需要在地图上绘制多边形要素,由于这个要素可以是不规则的,然后在要素的内部填充文字信息,尽量在多边形的中心绘制,所以要获取多边形的边界,就用到了openlayers框架中的ol.extent.boundingExtent()

API中方法介绍如下:

ol.extent.boundingExtent(coordinates)  //coordinates类型属于Array.<ol.Coordinate>,返回值为[minx, miny, maxx, maxy]类型的

详细的代码如下所示:

$.ajax({
        type: "get",
        url: url,
        dataType: "json",
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        async: true,
        xhrFields: {
            widthCredentials: true
        },
        success: function (data) {
            var len = data.features.length;//获取要素数组的长度
            layer.getSource().clear();
            layer_text.getSource().clear();
            //设置查询后的图层缩放层级
            for (var m = 0; m < demo.list.length; m++) {
                for (var i = 0; i < len; i++) {
                    if (demo.list[m].gdcode == data.features[i].properties["name"]) {
                        //读取地图上的要素
                        var coordinate = data.features[i].geometry.coordinates;
                        //获取填充文字的坐标位置
                        var coor_bound = ol.extent.boundingExtent(coordinate[0]);//获取一个坐标数组的边界
                        // console.log(coor_bound);//获得图形的边界,各个点的坐标,横坐标点最大值,最小值,纵坐标的最大值最小值
                        var coor_date_x_min = coor_bound[0];
                        var coor_x_dis = (coor_bound[2] - coor_bound[0])/2;
                        var coor_date_y_min = coor_bound[1];
                        var coor_y_dis = (coor_bound[3] - coor_bound[1]) / 2;
                        var coor_num = [coor_date_x_min+coor_x_dis,coor_date_y_min+coor_y_dis];
                        // 添加监测值
                        var anchor_value = new ol.Feature({
                            geometry: new ol.geom.Point(coor_num)
                        });
                        // 设置文字style
                        anchor_value.setStyle(new ol.style.Style({
                            text: new ol.style.Text({
                                font: '15px Microsoft YaHei',
                                text: data.features[i].properties["Leq2013"] + 'dB(A)',
                                fill: new ol.style.Fill({
                                    color: '#222'
                                })
                            })
                        }));
                        layer_text.getSource().addFeature(anchor_value);
                    }
                }
            }
            data = JSON.stringify(data);
            var features = new ol.format.GeoJSON().readFeatures(data);
            layer.getSource().addFeatures(features);
        },
        error: function (data) {
            console.log(data); //输出错误信息
        }
    });

上段代码中的var coor_bound = ol.extent.boundingExtent(coordinate[0]); 其中的coordinate[0] 表示
(7) [Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2)],字符串序列化为下面的格式:
[[120.61700857758387,31.26619926018776],[120.6084950447106,31.26619926018776],[120.6084950447106,31.273775421598486],[120.61731533453508,31.273775421598486],[120.61731533453508,31.270957736810146],[120.61642875228517,31.270112585427512],[120.61700857758387,31.26619926018776]] ,
然后输出coor_bound为
[120.56358934177479, 31.273710995965757, 120.56975389852369, 31.27600493358085]表示横坐标的最小值,纵坐标的最小值,横坐标的最大值,纵坐标的最大值。。

猜你喜欢

转载自blog.csdn.net/u013594477/article/details/80422801
今日推荐