openlayers3(五)根据坐标点画圆、线、多边形

这个是上一篇博客中的电子围栏中的一部分功能,但是因为有点小坑啥的我自己就单独拎出来写了。所以我写的也算是基于上一篇:openLayers3(四)电子围栏—使用画图工具绘图

话不多说,代码附上

代码
class drawFence{
    
    //转换坐标点(多)
    transPoints(points) {
        let arr = points.split(';');
        let point = [];
        arr.forEach(item = > {
            let newPoint = item.split(',');
            point.push(newPoint)
        })
        let _points = point.map(item = > {
            item = [parseFloat(item[0]), parseFloat(item[1])]
            item = ol.proj.transform(item, 'EPSG:4326', 'EPSG:3857');
            return item;
        })
        return _points;
    }
    //转换圆的
    transPoint(point) {
        let item = point.split(',')
        item = [parseFloat(item[0]), parseFloat(item[1])]
        let _point = ol.proj.transform(item, 'EPSG:4326', 'EPSG:3857');
        return _point;
    }
    /**
     * 
     * @param {*} fenceId 围栏id
     * @param {*} center 中心坐标
     * @param {*} radius 半径
     * @param {*} name 围栏名
     */
    showCircle(fenceId, center, radius, name) {

        let centerPoint = this.transPoint(center)
        radius = parseFloat(radius)
        var circleFeature = new ol.Feature({ //路线
            geometry: new ol.geom.Circle(centerPoint, radius),
        });
        circleFeature.setId(fenceId)
        //将所有矢量图层添加进去
        this.source.addFeature(circleFeature);
    }
    /**
     *
     * @param {*} fenceId 围栏id
     * @param {*} points 多边形坐标点
     * @param {*} name 围栏名
     */
    showPolygon(fenceId, points, name) {
        let _points = this.transPoints(points)
        _points = [_points]
        //多边形的数据格式是[[[lng,lat],[lng,lat]……]]外围两个中括号
        var polygonFeature = new ol.Feature({ //路线
            geometry: new ol.geom.Polygon(_points)

        });
        polygonFeature.setId(fenceId)
        this.source.addFeature(polygonFeature);
        console.log(this.source.getFeatures())
    }
    /**
     * 
     * @param {*} fenceId 围栏id
     * @param {*} points 线型的点坐标
     * @param {*} name 围栏名字
     */
    showPolyline(fenceId, points, name) {

        let _points = this.transPoints(points)
        var lineFeature = new ol.Feature({ //路线
            geometry: new ol.geom.LineString(_points, 'XY'),
        });
        lineFeature.setId(fenceId)

        //将所有矢量图层添加进去
        this.source.addFeature(lineFeature);
    }
    //回显围栏
    showFence(obj) {
        let {
            fenceId,
            name,
            type,
            center,
            radius,
            points
        } = obj;
        if (type == 'polygon') {
            this.showPolygon(fenceId, points, name)
        }
        if (type == 'circle') {
            this.showCircle(fenceId, center, radius, name)
        }
        if (type == 'polyline') this.showPolyline(fenceId, points, name)
    }

}

这里不得不多说一句,这个多边形外边包着两个中括号,我一开始直接用的线性坐标点格式,死活都出不来,还是在上一篇画图那里画多边形成功后的回调中获取坐标点才发现他外边有两层。

使用

$('#showPolygonFence').on('click', function () {
    var obj = {
        fenceId: '12',
        name: '围栏1',
        center: '',
        radius: '',
        type: 'polygon',
        points: '113.963223,22.549299;113.969655,22.553504;113.972242,22.547797;113.967176,22.544425;113.95945,22.544959;113.95981,22.551235;113.95981,22.551235'
    }
    fence.showFence(obj)
})
$('#showCircleFence').on('click', function () {
    var obj = {
        fenceId: '121',
        name: '围栏2',
        center: '113.959127,22.552036',
        radius: '152.96',
        type: 'circle',
        points: ''
    }
    fence.showFence(obj)
})
$('#showPolygonFence').on('click', function () {
    var obj = {
        fenceId: '12',
        name: '围栏3',
        center: '',
        radius: '',
        type: 'polyline',
        points: '113.960623,22.546082;113.958197,22.544029;113.956526,22.543245;113.953562,22.544563'
    }
    fence.showFence(obj)
})
效果

效果

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
### 回答1: OpenLayers 5 是一个开源的 JavaScript 库,用于创建交互式地图应用。要实现在地图上绘制坐标之间的连线,可以按照以下步骤进行: 1. 首先,在 HTML 文件中导入 OpenLayers 库和样式文件: ```html <link rel="stylesheet" href="https://openlayers.org/en/v5.3.0/css/ol.css" /> <script src="https://openlayers.org/en/v5.3.0/build/ol.js"></script> ``` 2. 创建一个地图容器的 div 元素: ```html <div id="map" style="width: 400px; height: 300px;"></div> ``` 3. 在 JavaScript 文件中,创建一个用于显示地图的 map 对象: ```javascript var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([0, 0]), zoom: 2 }) }); ``` 4. 创建一个矢量图层和一个矢量源对象: ```javascript var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector() }); map.addLayer(vectorLayer); var vectorSource = vectorLayer.getSource(); ``` 5. 为地图添加单击事件,获取击位置的坐标: ```javascript map.on('click', function(event) { var coordinate = event.coordinate; // 在此处可以将坐标添加到 vectorSource 中 }); ``` 6. 在单击事件的回调函数中,创建一个要素并添加到矢量源对象中: ```javascript var pointFeature = new ol.Feature({ geometry: new ol.geom.Point(coordinate) }); vectorSource.addFeature(pointFeature); ``` 7. 如果要连接这些坐标,可以使用 ol.geom.LineString 创建一个线要素,然后添加到矢量源对象中: ```javascript var lineStringFeature = new ol.Feature({ geometry: new ol.geom.LineString(coordinates) }); vectorSource.addFeature(lineStringFeature); ``` 最后,你将在地图上看到所有的坐标以及它们之间的连线。这是使用 OpenLayers 5 实现坐标线的基本步骤。可以通过进一步自定义样式和处理事件来增加功能。 ### 回答2: OpenLayers 5是一个开源的JavaScript库,可用于在Web上实现地图功能。如果要在地图上绘制坐标之间的连线,可以按照以下步骤进行: 1. 初始化地图:首先,需要在页面上创建一个地图容器,然后初始化地图对象。可以指定地图的中心坐标、缩放级别等属性。 2. 添加图层:接下来,创建一个矢量图层,并将其添加到地图中。这样可以在地图上绘制矢量要素,如线、面等。 3. 创建要素:使用OpenLayers的Point类,可以创建一个要素,其中包含要绘制的坐标的经纬度信息。 4. 创建线要素:使用OpenLayers的LineString类,可以创建一个线要素,其中包含要绘制的坐标序列。 5. 绘制要素:将要素添加到图层中,再将线要素添加到图层中。这样,地图上就可见了这些要素。 以下是一个示例代码片段,展示了如何在OpenLayers 5中实现坐标线的功能: ```javascript // 初始化地图 var map = new ol.Map({ target: 'map-container', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([0, 0]), zoom: 10 }) }); // 创建矢量图层 var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector() }); map.addLayer(vectorLayer); // 创建要素 var point1 = new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([lon1, lat1])) }); var point2 = new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([lon2, lat2])) }); // 创建线要素 var lineFeature = new ol.Feature({ geometry: new ol.geom.LineString([ ol.proj.fromLonLat([lon1, lat1]), ol.proj.fromLonLat([lon2, lat2]) ]) }); // 添加要素到图层 vectorLayer.getSource().addFeature(point1); vectorLayer.getSource().addFeature(point2); vectorLayer.getSource().addFeature(lineFeature); ``` 以上代码中的`map-container`是在HTML页面上用来容纳地图的一个元素,`lon1`、`lat1`、`lon2`和`lat2`是两个坐标的经纬度值。通过将要素和线要素添加到矢量图层中,即可在地图上看到这些要素,实现坐标的划线功能。 ### 回答3: 使用OpenLayers 5实现坐标线的方法如下: 首先,创建一个地图容器,可以使用HTML中的一个div元素作为容器: ```html <div id="map" style="width: 100%; height: 500px;"></div> ``` 然后,在JavaScript中,创建一个OpenLayers地图实例并将其显示在地图容器中: ```javascript var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([0, 0]), zoom: 4 }) }); ``` 接下来,创建一个用于存储坐标的数组: ```javascript var coordinates = [ [0, 0], // 第一个的经纬度 [10, 10], // 第二个的经纬度 [20, 20], // 第三个的经纬度 // ... ]; ``` 然后,使用OpenLayers的Vector模块创建一个特征集合,并将坐标添加到特征集合中: ```javascript var features = new ol.Collection(); var feature = new ol.Feature({ geometry: new ol.geom.LineString(coordinates) }); features.push(feature); ``` 再创建一个用于显示特征集合的矢量层: ```javascript var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ features: features }) }); ``` 最后,将矢量层添加到地图上,就可以在地图上显示坐标的划线了: ```javascript map.addLayer(vectorLayer); ``` 需要注意的是,以上代码只是一个简单示例,实际应用中您可能需要根据具体需求调整参数和添加其他功能。此外,还需要确保引入了OpenLayers的JavaScript文件。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值