【源码】Vue2+openLayers实现:测量长度,测量面积

版权声明:文为博主原创文章,未经博主允许不得转载。原创不易,希望大家尊重原创!
Copyright © 2024 DarLing丶张皇 保留所有权利

一、引言

在 Web GIS 应用中,测量工具是不可或缺的功能之一。它允许用户在地图上测量两点之间的距离或绘制多边形来计算面积。通过在 OpenLayers 中实现这些工具,开发者可以提高地图的交互性和实用性。本文将详细介绍如何在 Vue 单页面应用中使用 OpenLayers 实现距离和面积测量功能。

1、效果展示

测量距离和测量面积

2、OpenLayers 测量工具实现方法

在 OpenLayers 中,测量工具主要通过 ol/interaction/Draw 交互来实现,配合ol/sphere模块可以计算几何图形的长度和面积。开发者可以在 Vue 组件中封装这些功能,使应用更具交互性和可扩展性。

3、参考资源

官网Measure链接: link.

二、具体实现

1、安装OpenLayers

npm install ol

2、创建MeasureTool.js的工具类

/**
 * @fileName: openlayers地图 测量工具 MeasureTool.js
 * @copyright: DarLing丶张皇
 * @date: 2024-12-13 10:24:11
 */
import 'ol/ol.css';
import {
   
    Map } from 'ol/Map.js';
import {
   
    View } from 'ol/View.js';
// import { Draw, Modify, Snap } from 'ol/interaction';
import Draw from 'ol/interaction/Draw';
import VectorLayer from "ol/layer/Vector";
import VectorSource from 'ol/source/Vector';
import Overlay from 'ol/Overlay.js';
import {
   
    LineString, Polygon, Point } from 'ol/geom';
import {
   
    getArea, getLength } from 'ol/sphere';
import {
   
    Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style';
import {
   
    unByKey } from 'ol/Observable.js';

/**
 * 测量工具类
 */
class MeasurementTool {
   
   
  map;//地图对象
  sketch = null;//当前要素
  helpTooltipElement = null;//帮助提示框元素
  helpTooltip = null;
  measureTooltipElement = null;// 测量提示元素
  measureTooltip = null;//测量值
  continuePolygonMsg = '单击以继续绘制多边形';//绘制面提示文字
  continueLineMsg = '单击以继续绘制线条';//绘制线提示文字
  
  constructor(map) {
   
   
    this.map = map;
    this.source = new VectorSource();
    this.vectorLayer = new VectorLayer({
   
   
      source: this.source,
      style: new Style({
   
   
        fill: new Fill({
   
   
          color: 'rgba(255, 255, 255, 0.2)',
        }),
        stroke: new Stroke({
   
   
          color: '#ffcc33',
          width: 2,
        }),
        image: new CircleStyle({
   
   
          radius: 7,
          fill: new Fill({
   
   
            color: '#ffcc33',
          }),
        }),
      }),
      zIndex: 99,// 层级
    });
    this.map.addLayer(this.vectorLayer);
    
    // 监听鼠标移动
    this.map.on('pointermove', this.pointerMoveHandler.bind(this));
    this.map.getViewport().addEventListener('mouseout', () => {
   
   
      this.helpTooltipElement.classList.add('hidden');
    });
  }

  pointerMoveHandler(evt) {
   
   
    if (evt.dragging) {
   
   
      return;
    }
    let helpMsg = '点击开始绘图';
    if 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值