当前位置: 首页 > news >正文

【openlayers框架学习】七:绘制线要素以及点击画线功能


文章目录

    • openlayers入门
      • 25 openlayers添加线要素(Feature->geometry:LineString)
      • 26 点击画线功能


openlayers入门

25 openlayers添加线要素(Feature->geometry:LineString)

main.js

import './style.css';
import Map from 'ol/Map';
import 'ol/ol.css';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import { XYZ } from 'ol/source';
import VectorLayer from 'ol/layer/Vector.js';
import Style from 'ol/style/Style';
import Feature from 'ol/Feature';
import { LineString, Point } from 'ol/geom';
import { Vector } from "ol/source";
import Icon from 'ol/style/Icon';
import CircleStyle from 'ol/style/Circle.js';
import Fill from 'ol/style/Fill';
import Text from 'ol/style/Text.js';
import Stroke from 'ol/style/Stroke';
import VectorSource from 'ol/source/Vector';
const center = [114.25, 30.59];
const view = new View({center: center,zoom: 4,projection: "EPSG:4326",
});
//城市矢量地图-高德地图瓦片
const gaodeSource = new XYZ({url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
});
const gaodeLayer = new TileLayer({source: gaodeSource,
});
const map = new Map({target: "map",view: view,layers: [gaodeLayer],
});
//绘制线要素 画一条从武汉到北京的的直线
const lineFeature = new Feature({geometry: new LineString([[114.25,30.59],[116.46,39.92]]),
});
lineFeature.setStyle(new Style({stroke: new Stroke({color: "red",}),}),
);
const vectorSource = new VectorSource({features:[lineFeature],
});
const vectorLayer = new VectorLayer({source:vectorSource,
});
map.addLayer(vectorLayer);

26 点击画线功能

index.html

<!doctype html>
<html lang="en"><head><meta charset="UTF-8" /><link rel="icon" type="image/svg+xml" href="/vite.svg" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Vite App</title></head><body><div id="map"></div><script type="module" src="/src/main.js"></script></body>
</html>

main.js

import './style.css';
import Map from 'ol/Map';
import 'ol/ol.css';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import { XYZ } from 'ol/source';
import VectorLayer from 'ol/layer/Vector.js';
import Style from 'ol/style/Style';
import Feature from 'ol/Feature';
import { LineString, Point } from 'ol/geom';
import { Vector } from "ol/source";
import Icon from 'ol/style/Icon';
import CircleStyle from 'ol/style/Circle.js';
import Fill from 'ol/style/Fill';
import Text from 'ol/style/Text.js';
import Stroke from 'ol/style/Stroke';
import VectorSource from 'ol/source/Vector';
const center = [114.25, 30.59];
const view = new View({center: center,zoom: 4,projection: "EPSG:4326",
});
//城市矢量地图-高德地图瓦片
const gaodeSource = new XYZ({url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
});
const gaodeLayer = new TileLayer({source: gaodeSource,
});
const map = new Map({target: "map",view: view,layers: [gaodeLayer],
});
// //绘制线要素 画一条从武汉到北京的的直线
// const lineFeature = new Feature({
//     geometry: new LineString([
//         [114.25,30.59],
//         [116.46,39.92]
//     ]),
// });
// lineFeature.setStyle(
//     new Style({
//         stroke: new Stroke({
//             color: "red",
//         }),
//     }),
// );
const vectorSource = new VectorSource({features:[],
});
const vectorLayer = new VectorLayer({source:vectorSource,
});
map.addLayer(vectorLayer);
let lineArr = [];
map.on('click',function(e){//去掉之前的要素vectorSource.clear();let coordinate = e.coordinate;lineArr.push(coordinate);if(lineArr.length === 2){let lineFeature = new Feature({geometry: new LineString(lineArr),});lineFeature.setStyle(new Style({stroke: new Stroke({color: "red",width: 8,}),}));vectorSource.addFeature(lineFeature);lineArr = [];}});

style.css

*{padding: 0;margin: 0;box-sizing: border-box;
}
#map{width: 100vw;height: 100vh;
}
http://www.dtcms.com/a/313409.html

相关文章:

  • 力扣热题100----------141.环形链表
  • 基于BiLSTM+CRF实现NER
  • 【机器人】VLN-R1 微调 | 增强训练 | 连续导航
  • Web3合约ABI,合约地址生成部署调用及创建,连接钱包,基础交易流程
  • ARPO:让LLM智能体更高效探索
  • 【Linux网络编程基础--socket地址API】
  • 多 4G 通讯模组共存时的干扰问题深度解析与解决方案
  • leecode-每日一题-2106. 摘水果
  • vmfusion启动centos6.10 一直卡到call 169.254.169.254
  • 全面解析 BGE Embedding 模型:训练方式、模型系列与实战用法
  • Redis——常用指令汇总指南(三)(哈希类型)
  • 编写xsync集群分发脚本(保姆级别)
  • Redis 数据同步机制
  • 【Linux】Makefile Cmake—基操
  • [特殊字符]字节Get!免费进楼攻略速存[特殊字符]
  • LWIP从FreeRTOS到uC/OS-III的适配性改动
  • linux 扩展未分配的磁盘空间到home下
  • SQL157 更新记录(一)
  • 代码随想录算法训练营第五十八天|动态规划part8
  • 成功解决ImportError: DLL load failed while importing _multiarray_umath: 找不到指定的模块。
  • 深度学习中的模型知识蒸馏
  • 深度学习中卷积与互相关
  • 记录使用ruoyi-flowable开发部署中出现的问题以及解决方法
  • FastAPI-Vue3-Admin 一款Python 全栈融合的高可用中后台快速开发平台方案
  • golang 函数选项模式
  • 数据结构(概念及链表)
  • 【数据结构】队列的顺序存储与链式存储(C语言版)
  • 基于深度学习的医学图像分析:使用变分自编码器(VAE)实现医学图像生成
  • (FD Conv)Frequency Dynamic Convolution for Dense Image Prediction论文精读(逐段解析)
  • 07.config 命令实现动态修改配置和慢查询