openlayers6绘制点线面圆

gis中点线面的绘制是常用功能。一般在交通项目中会用来绘制限行区,公安项目会用来绘制辖区,以及在资源搜索时,会用到点选、圈选、多边形选等。
在这里插入图片描述
在这里插入图片描述

1、HTML——用到了iview框架的下拉框组件

<template>
    <div class="box">
        <div class="operation">
            <Dropdown style="margin-left: 15px"  trigger="click" @on-click="typeSelectChange">
                <span>
                    绘制
                    <Icon type="ios-arrow-down"></Icon>
                </span>
                <DropdownMenu slot="list">
                    <DropdownItem name="Point"></DropdownItem>
                    <DropdownItem name="LineString">线</DropdownItem>
                    <DropdownItem name="Box">矩形</DropdownItem>
                    <DropdownItem name="Polygon">多边形</DropdownItem>
                    <DropdownItem name="Circle"></DropdownItem>
                    <DropdownItem name="clear">清除</DropdownItem>
                </DropdownMenu>
            </Dropdown>
        </div>
        <div class="info" ref="infoEl">

        </div>
        <div id="map"></div>
    </div>
</template>

2、JavaScript——底图用的是arcgis蓝色地图;并将绘制的坐标显示在页面;采取的是不连续绘制。

<script>
/* eslint-disable */
import {Map, View} from 'ol';
import Tile from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import TileGrid from 'ol/tilegrid/TileGrid';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import {Icon, Style,Stroke,Fill,Circle} from 'ol/style';
import Draw from "ol/interaction/Draw";
import {createBox} from "ol/interaction/Draw";
import {fromLonLat} from 'ol/proj';


export default {
	name: "home",
	components:{

	},
	data(){
		return {

        }
	},
	mounted(){
        let center = [108.946994, 34.261361];

        // arcgiS图层
        let arcgisLayer = new Tile({
            source: new XYZ({
                url: 'https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}'
            }),
        });
        // 矢量图层
        this.vectorLayer = new VectorLayer({
            source: new VectorSource(),
            style: new Style({
                
                stroke: new Stroke({
                    width: 2,
                    color: [242,114,60, 1],
                    lineDash:[1,2,3,4,5,6],
                }),
                fill: new Fill({
                    color: [242,114,60, 0.2],
                }),
                image: new Circle({
                    // 点的颜色
                    fill: new Fill({
                        color: [242,114,60, .5]
                    }),
                    stroke: new Stroke({
                        color: [242,114,60, 1]
                    }),
                    // 圆形半径
                    radius: 5
                }),
                
            })
        })
        this.map = new Map({
            target: "map",
            layers: [arcgisLayer,this.vectorLayer],
            view: new View({
                center: center,
                zoom: 12,
                projection: "EPSG:4326",
            }),
        });
    },
	methods:{
        typeSelectChange(value) {
            this.map.removeInteraction(this.draw);
            if (value === 'clear') {
                this.vectorLayer.getSource().clear();
                this.$refs.infoEl.innerHTML = "";
            }else{
                if(value === "Box"){
                    this.draw = new Draw({
                        source: this.vectorLayer.getSource(),
                        geometryFunction: createBox(),
                        type: "LineString",
                        stopClick: true,  //绘制时禁用点击事件
                        style:this.getDrawingStyle(),
                    })
                }else{
                    this.draw = new Draw({
                        source: this.vectorLayer.getSource(),
                        type: value,
                        stopClick: true,  //绘制时禁用点击事件
                        style:this.getDrawingStyle(),
                    })
                }
                
                this.draw.on("drawend",(e)=>{
                    console.log(window.e=e,window.d=this.draw);
                    if(this.draw.type_ === "Circle"){
                        let circleCenter = e.feature.getGeometry().getCenter();
                        let circleRadius = e.feature.getGeometry().getRadius();
                        circleRadius = fromLonLat([circleRadius,circleRadius],"EPSG:3857")[0];
                        this.$refs.infoEl.innerHTML = "圆心:" + circleCenter+"<br/>半径:"+circleRadius;
                    }else{
                        let coordinates = e.feature.getGeometry().getCoordinates();
                        this.$refs.infoEl.innerHTML = "坐标:"+coordinates;
                    }
                    this.map.removeInteraction(this.draw);
                });
                this.map.addInteraction(this.draw);
            }
        },
        getDrawingStyle(){
            return new Style({
                stroke: new Stroke({
                    width: 2,
                    color: [239,176,19, 1],
                    lineDash:[1,2,3,4,5,6],
                }),
                fill: new Fill({
                    color: [239,176,19, 0.2],
                }),
                image: new Circle({
                    // 点的颜色
                    fill: new Fill({
                        color: [239,176,19, .5]
                    }),
                    stroke: new Stroke({
                        color: [239,176,19, 1]
                    }),
                    // 圆形半径
                    radius: 5
                }),
            })
        },
	},
};
</script>

3、css

<style scoped lang="less">
.box {
    width: 100%;
	height: 100%;
    position: relative;
    .operation{
		position: absolute;
		z-index: 1;
		top: 10px;
		left: 10px;
		display: flex;
		background: #fff;
		color: #000;
		border-radius: 2px;
		box-shadow: 1px 2px 1px rgba(0,0,0,.15);
		padding:5px 15px 5px 10px;
    }
    .info{
        position: absolute;
        z-index: 1;
        top: 10px;
        right: 10px;
        background: #fff;
        color: #000;
        max-width: 200px;
        word-break: break-all;
    }
	#map{
		width:100%;
		height: 100%;
		/deep/ .ol-control{
			display:none;
		}
	}
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
OpenLayers是一个开源的JavaScript库,用于在Web上渲染交互式地图。它提供了丰富的功能和API,允许用户在地图上添加各种不同的元素,包括点、线和面。 以下是OpenLayers绘制点、线和面的基本步骤: 1. 创建地图对象 使用OpenLayers创建一个地图对象,设置地图中心点和缩放级别。 2. 创建矢量图层 使用OpenLayers创建一个矢量图层,并将其添加到地图中。 3. 创建要素 使用OpenLayers创建一个要素对象,可以是点、线或面。 4. 绘制要素 使用OpenLayers提供的绘制工具,将要素添加到矢量图层中。可以通过鼠标交互或代码方式来进行绘制。 5. 渲染地图 将地图渲染到页面上,可以使用OpenLayers提供的默认样式,也可以自定义样式。 下面是一个简单的示例代码,演示如何使用OpenLayers绘制点、线和面: ``` // 创建地图对象 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([116.38, 39.9]), zoom: 10 }) }); // 创建矢量图层 var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector(), style: new ol.style.Style({ fill: new ol.style.Fill({ color: 'rgba(255, 255, 255, 0.2)' }), stroke: new ol.style.Stroke({ color: '#ffcc33', width: 2 }), image: new ol.style.Circle({ radius: 7, fill: new ol.style.Fill({ color: '#ffcc33' }) }) }) }); map.addLayer(vectorLayer); // 创建要素 var point = new ol.geom.Point(ol.proj.fromLonLat([116.38, 39.9])); var line = new ol.geom.LineString([ol.proj.fromLonLat([116.38, 39.9]), ol.proj.fromLonLat([116.4, 39.9])]); var polygon = new ol.geom.Polygon([[ ol.proj.fromLonLat([116.38, 39.9]), ol.proj.fromLonLat([116.4, 39.9]), ol.proj.fromLonLat([116.4, 39.92]), ol.proj.fromLonLat([116.38, 39.92]), ol.proj.fromLonLat([116.38, 39.9]) ]]); // 绘制要素 var pointFeature = new ol.Feature(point); var lineFeature = new ol.Feature(line); var polygonFeature = new ol.Feature(polygon); vectorLayer.getSource().addFeatures([pointFeature, lineFeature, polygonFeature]); // 渲染地图 map.render(); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值