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

相关文章

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoo WebFlux+MongoDB实现非阻塞API过程

《SpringBooWebFlux+MongoDB实现非阻塞API过程》本文介绍了如何使用SpringBootWebFlux和MongoDB实现非阻塞API,通过响应式编程提高系统的吞吐量和响应性能... 目录一、引言二、响应式编程基础2.1 响应式编程概念2.2 响应式编程的优势2.3 响应式编程相关技术

C#实现将XML数据自动化地写入Excel文件

《C#实现将XML数据自动化地写入Excel文件》在现代企业级应用中,数据处理与报表生成是核心环节,本文将深入探讨如何利用C#和一款优秀的库,将XML数据自动化地写入Excel文件,有需要的小伙伴可以... 目录理解XML数据结构与Excel的对应关系引入高效工具:使用Spire.XLS for .NETC

C++ 右值引用(rvalue references)与移动语义(move semantics)深度解析

《C++右值引用(rvaluereferences)与移动语义(movesemantics)深度解析》文章主要介绍了C++右值引用和移动语义的设计动机、基本概念、实现方式以及在实际编程中的应用,... 目录一、右值引用(rvalue references)与移动语义(move semantics)设计动机1

Nginx更新SSL证书的实现步骤

《Nginx更新SSL证书的实现步骤》本文主要介绍了Nginx更新SSL证书的实现步骤,包括下载新证书、备份旧证书、配置新证书、验证配置及遇到问题时的解决方法,感兴趣的了解一下... 目录1 下载最新的SSL证书文件2 备份旧的SSL证书文件3 配置新证书4 验证配置5 遇到的http://www.cppc

Nginx之https证书配置实现

《Nginx之https证书配置实现》本文主要介绍了Nginx之https证书配置的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起... 目录背景介绍为什么不能部署在 IIS 或 NAT 设备上?具体实现证书获取nginx配置扩展结果验证