移动端openlayers封装绘制Draw-随手画-矩形-圆

移动端openlayers封装绘制Draw-随手画-矩形-圆

如果直接使用官方示例的教程,在移动端绘制没法按压拖拽绘制,百度半天也是没有找到,解决了做一个笔记。

矩形:
在这里插入图片描述
随手画:
在这里插入图片描述
DrawArea.js代码:

/**
 * 检索组件绘制方法封装: DrawArea
 */
import Draw from 'ol/interaction/Draw.js';
import {
	OSM,
	Vector as VectorSource
} from 'ol/source.js';
import {
	Tile as TileLayer,
	Vector as VectorLayer
} from 'ol/layer.js';
import {
	Circle,
	Fill,
	Stroke,
	Style,
} from 'ol/style';
import {
	Polygon
} from '../ol/geom.js';
import {
	createRegularPolygon
} from 'ol/interaction/Draw.js';

const drawTypeEnum = {
	0: 'None', // 不绘制
	1: 'Point', // 绘制点
	2: 'Polygon', // 绘制多边形(移动端不适用)
	3: 'Circle', // 绘制圆(边为很多)
	4: 'Circle', // 绘制矩形(边为4)
	// 随手画,增加下面的函数控制,即可为拖拽绘制
	// freehandCondition: (event) => {
	// 	// 按压拖拽绘制
	// 	return event.originalEvent.button == -1 || event.originalEvent.button === 0;
	// },
	5: 'Polygon',

}

export class DrawArea {

	map;

	draw;

	projection;

	mapServiceLoader;

	drawSource;

	drawLayer;

	// 自定义参数
	startPoint = null;
	drawing = false;

	/**
	 * 清除图层的数据
	 */
	clearDrawLayer() {
		this.drawLayer.getSource().clear();
		this.endDraw();
	}

	/**
	 * 结束绘制
	 */
	endDraw() {
		if (this.draw) {
			this.map.removeInteraction(this.draw);
			this.draw = null;
		}
	}

	/**
	 * 开始绘制
	 */
	start(type = 3) {
		const drawType = drawTypeEnum[type];
		if (drawType !== 'None') {
			const drawStyle = new Style({
				fill: new Fill({
					color: 'rgba(172, 168, 85, 0.5)',
				}),
				stroke: new Stroke({
					color: 'red',
					width: 1,
				}),
			});
			if (this.draw) {
				this.map.removeInteraction(this.draw);
			}
			let draw = null;
			switch (type) {
				case 0:
					// 清除绘制
					draw = null;
					this.clearDrawLayer();
					break;
				case 1:
					// 绘制点
					draw = new Draw({
						source: this.drawSource,
						// style: drawStyle,
						type: 'Point',
					});
					break;
				case 2:
					// 绘制多边形
					draw = new Draw({
						source: this.drawSource,
						// style: drawStyle,
						type: 'Polygon',
						freehandCondition: (event) => {
							// 按压拖拽绘制-------随手画的关键代码就是这个增加freehandCondition
							return event.originalEvent.button == -1 || event.originalEvent.button === 0;
						},
					});
					break;
				case 3:
					//  绘制圆(边为很多)
					draw = new Draw({
						source: this.drawSource,
						// style: drawStyle,
						type: 'Circle',
						geometryFunction: createRegularPolygon(32),
						freehandCondition: (event) => {
							// 按压拖拽绘制
							return event.originalEvent.button == -1 || event.originalEvent.button === 0;
						},
					});
					break;
				case 4:
					// 绘制矩形
					draw = new Draw({
						source: this.drawSource,
						// style: drawStyle,
						type: 'Circle',
						geometryFunction: createRegularPolygon(4),
						freehandCondition: (event) => {
							// 按压拖拽绘制
							return event.originalEvent.button == -1 || event.originalEvent.button === 0;
						},
					});
					break;
				case 5:
					// 随手画
					draw = new Draw({
						source: this.drawSource,
						// style: drawStyle,
						type: 'Polygon',
						freehandCondition: (event) => {
							// 按压拖拽绘制
							return event.originalEvent.button == -1 || event.originalEvent.button === 0;
						},
					});
					break;
				default:
					return;
			}
			this.draw = draw;
			this.map.addInteraction(this.draw);

			// 监听绘制完成事件
			this.draw.on('drawend', (event) => {
				// 获取绘制的要素
				console.log('监听绘制完成事件', event, this.layer);
				const {
					feature
				} = event;
				this.drawLayer.getSource().addFeature(feature);
			});
		} else {
			// 移除绘制
		}
	}

	/**
	 * 创建一个图层用来绘制
	 */
	createDrawLayer() {
		const style = new Style({
			fill: new Fill({
				color: 'rgba(255, 255, 255, 0.5)',
			}),
			stroke: new Stroke({
				color: '#0099ff',
				width: 3,
			}),
		});
		const layer = new VectorLayer({
			source: new VectorSource({}),
			style: function() {
				return style;
			},
			zIndex: 99999,
		});
		// layer.setId('drawLayer');
		this.drawLayer = layer;
		this.map.addLayer(layer);
	}

	/**
	 * @param {Object} map
	 * @param {Object} projection
	 * @param {Object} mapServiceLoader
	 * @param {Object} drawSource
	 */
	constructor(map, projection, mapServiceLoader) {
		this.map = map;
		this.projection = projection;
		this.mapServiceLoader = mapServiceLoader;
		this.drawSource = new VectorSource({
			wrapX: false
		});;
		this.createDrawLayer();
	}
}

使用:

import {
		DrawArea
	} from '@/map/DrawArea.js';

// 这里示例-只放了部分代码
const map = new Map({
					target: this.mapId,
					layers: [],
					view,
					controls: [],
		});

// 创建绘制交互
this.DrawArea = new DrawArea(map, projection, this.mapServiceLoader);




// 检索组件相关参数改变-开始绘制
			spTypeAndDrawChange(e) {
				console.log('检索组件相关参数改变', e);
				this.spTypeAndDrawData = e;
				const {
					regionDrawType,
					spatialPositionType
				} = e;
				if (spatialPositionType === 1) {
					if (regionDrawType === 0) {
						// 清除
						this.DrawArea.clearDrawLayer();
					} else {
						// 禁止拖动地图
						// 绘制模式
						this.DrawArea.start(regionDrawType);
					}
				} else {
					// 清除
					this.DrawArea.clearDrawLayer();
				}
			},

ok

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星月前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值