openlayers6实现可拖拽点位,点位可拖拽移动点击移动

2024-01-22 02:32

本文主要是介绍openlayers6实现可拖拽点位,点位可拖拽移动点击移动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

openlayers6实现可拖拽点位

实现功能:需要实现地图上点击出现点位,此点位可拖拽,并且点击移动,从而实现地图选点的功能

使用技术:openlayers6 + react + 高德地图底图

1. 第一步:初始化地图容器

html代码:新增dom节点

<div id="testMap" style={{width: '100vw', height: '100vh'}}></div>

2. 第二步:初始化地图

js代码:初始化时调用方法初始化高德地图

loadMap = () =>{// 添加点位图层var vectorSource = new VectorSource({features: [],});this.vectorLayer = new VectorLayer({source: vectorSource,zIndex: 100});// 需要转换坐标系 - 将4326转化为3857坐标系let m_center = [lngCenter, latCenter];//地图中心点-经纬度坐标m_center = olProj.transform(m_center, 'EPSG:4326', 'EPSG:3857');// 使用高德道路图this.map = new Map({target: 'testMap',layers: [new TileLayer({source: new XYZ({url: mapURL["aMap-vec-d"]})}),this.vectorLayer],view: new View({center: m_center,zoom: 10,minZoom: 5,maxZoom: 18,expandZoomRange: true,projection: 'EPSG:3857'}),});// 调用选点方法this.mapClick();
}

3. 第三步:增加地图选点方法

mapClick = () => {this.map.on('singleclick', (e) => {// 获取当前点位坐标let point = e.coordinate;if(this.iconFeature){// 将地图回中// this.map.getView().setCenter(point);// 将点位清除再绘制// this.vectorLayer.getSource().clear();// 直接修改点位位置this.iconFeature.setGeometry(new Point([point[0], point[1]]));// 将点位转成EPSG:4326坐标系let clickPoint = olProj.transform(point, 'EPSG:3857', 'EPSG:4326');console.log(clickPoint);}else{// 新增点位Featurethis.iconFeature = new Feature({   geometry: new Point([point[0], point[1]]),name: 'Map Point',population: 4000,rainfall: 500,})// 为Feature增加样式let iconStyle = new Style({image: new Icon({anchor: [0.5, 1],// 图片地址src: require('./../../image/map_point.png'),}),});this.iconFeature.setStyle(iconStyle);// 在点位layer中加上Featurethis.vectorLayer.getSource().addFeature(this.iconFeature);// 增加可移动点位元素let iconTranslate = new PointerTranslate({features: new Collection([this.iconFeature])})this.map.addInteraction(iconTranslate);// 为元素增加事件iconTranslate.on("translateend", () => {let clickPoint = this.iconFeature.getGeometry().flatCoordinates;// 将点位转成EPSG:4326坐标系clickPoint = olProj.transform(clickPoint, 'EPSG:3857', 'EPSG:4326');console.log(clickPoint);})}})
}

效果图
点击出现点位
点击打印出点位,拖拽点位也打印出点位
控制台

全部代码

import React from 'react';import TileGrid from "ol/tilegrid/TileGrid";
import TileImage from "ol/source/TileImage";
import Map from 'ol/Map';
import View from 'ol/View';
import XYZ from 'ol/source/XYZ';
import VectorSource from 'ol/source/Vector';
import Feature from 'ol/Feature';
import * as olProj from 'ol/proj';
import Point from 'ol/geom/Point';
import { Style, Icon } from 'ol/style';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
import { Translate as PointerTranslate } from 'ol/interaction';
import Collection from 'ol/Collection';import 'ol/src/ol.css';const lngCenter = "116.397428"
const latCenter = "39.90923"
const mapURL = {/***** 高德地图* lang可以通过zh_cn设置中文,en设置英文,size基本无作用,scl设置标注还是底图,scl=1代表注记,scl=2代表无注记,* style代表底图(矢量或者影像),style设置影像和路网,style=6为影像图,* vec——街道底图* img——影像底图* roadLabel---路网+标注*/"aMap-img-d": "http://webst0{1-4}.is.autonavi.com/appmaptile?style=6&scale=1&x={x}&y={y}&z={z}","aMap-img": "http://webst0{1-4}.is.autonavi.com/appmaptile?style=6&scale=2&x={x}&y={y}&z={z}","aMap-vec-d": "http://webrd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scl=1&style=8&x={x}&y={y}&z={z}","aMap-vec": "http://webrd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scl=2&style=8&x={x}&y={y}&z={z}","aMap-roadLabel": "http://webst0{1-4}.is.autonavi.com/appmaptile?style=8&x={x}&y={y}&z={z}"
}
class TestMap extends React.Component{constructor(props){super(props);}componentDidMount(){this.loadMap();}loadMap = () =>{// 添加点位图层var vectorSource = new VectorSource({features: [],});this.vectorLayer = new VectorLayer({source: vectorSource,zIndex: 100});// 需要转换坐标系 - 将4326转化为3857坐标系let m_center = [lngCenter, latCenter];//地图中心点-经纬度坐标m_center = olProj.transform(m_center, 'EPSG:4326', 'EPSG:3857');this.map = new Map({target: 'testMap',layers: [new TileLayer({source: new XYZ({url: mapURL["aMap-vec-d"]})}),this.vectorLayer],view: new View({center: m_center,zoom: 10,minZoom: 5,maxZoom: 18,expandZoomRange: true,projection: 'EPSG:3857'}),});this.mapClick();}/*** @name: mapClick* @desc: 点击地图出现点位图片 - 点位可移动*/mapClick = () => {this.map.on('singleclick', (e) => {// 获取当前点位坐标let point = e.coordinate;if(this.iconFeature){// 将地图回中// this.map.getView().setCenter(point);// 将点位清除再绘制// this.vectorLayer.getSource().clear();// 直接修改点位位置this.iconFeature.setGeometry(new Point([point[0], point[1]]));// 将点位转成EPSG:4326坐标系let clickPoint = olProj.transform(point, 'EPSG:3857', 'EPSG:4326');console.log(clickPoint);}else{// 新增点位Featurethis.iconFeature = new Feature({   geometry: new Point([point[0], point[1]]),name: 'Map Point',population: 4000,rainfall: 500,})// 为Feature增加样式let iconStyle = new Style({image: new Icon({anchor: [0.5, 1],src: require('./../../image/map_point.png'),}),});this.iconFeature.setStyle(iconStyle);// 在点位layer中加上Featurethis.vectorLayer.getSource().addFeature(this.iconFeature);// 增加可移动点位元素let iconTranslate = new PointerTranslate({features: new Collection([this.iconFeature])})this.map.addInteraction(iconTranslate);// 为元素增加事件iconTranslate.on("translateend", () => {let clickPoint = this.iconFeature.getGeometry().flatCoordinates;// 将点位转成EPSG:4326坐标系clickPoint = olProj.transform(clickPoint, 'EPSG:3857', 'EPSG:4326');console.log(clickPoint);})}})}render(){return(<div id="testMap" style={{width: '100vw', height: '100vh'}}></div>)}
}
export default TestMap

这篇关于openlayers6实现可拖拽点位,点位可拖拽移动点击移动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/631583

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal