openLayers实战(四):设置地图中心点、修改地图中心点

news2024/10/6 8:35:29

 截至到目前阶段的功能,我自己实现最完整的代码

import "ol/ol.css";
import Map from "ol/Map";
import Feature from "ol/Feature";
import VectorSource from "ol/source/Vector";
import Overlay from "ol/Overlay";
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer";
import View from "ol/View";
import { transform, fromLonLat } from "ol/proj";
import XYZ from "ol/source/XYZ";
import { Point, LineString } from "ol/geom";
import GeoJSON from "ol/format/GeoJSON";
import { Fill, Stroke, Icon, Style } from "ol/style";
import markerImg from "@/assets/images/fly.svg";
import markerCenter from "@/assets/images/centerPos.png";
import markerCurrent from "@/assets/images/curPos.png";


 data() {
    return {
      mapObj: null,
      mapDom: null,
      mapPointList: [],
      pointLayerSource: null,
      pointLayer: null, // 绘制地图中心点的图层
      markerIcon: markerImg,
      markerCenterIcon: markerCenter,
      markerCurrentIcon: markerCurrent,
      extent: [121.5509, 31.0865, 121.8109, 31.382],
      tileUrl: "http://192.168.1.103:8081" + "/{z}/{x}/{y}.jpg",
      routeLayer: {},
      featureMove: {},
      coordinates: [
        [121.70185, 31.298777],
        [121.70285, 31.296777],
        [121.70385, 31.295777],
        [121.70585, 31.294777],
      ],
      routeIndex: 0, //当前小车所在的路段
      carLayer: null,
      timer: null, // 计时器
      speed: null,
      coverPoints: [
        [121.70585, 31.294777],
        [121.70585, 31.294777],
      ],
      currentCoodinate: null,
      mapTypeFlag: false,
      centerPoint: { longitude: 121.70185, latitude: 31.298777 },
    };
  }, 
 mounted() {
    this.initMap();
    // this.openLine();

    // 设置 默认的中心点坐标
    this.addPoint(
      this.centerPoint,
      this.markerCenterIcon,
      "centerIconLayer",
      "centerIconFeatureId"
    );

    this.mapObj.on("click", (evt) => {
      // 获取当前点击点的 feature,如果存在就移除
      const feature = this.pointLayerSource.getFeatureById("curIconFeatureId");
      if (feature) {
        this.pointLayerSource.removeFeature(feature);
      }
      this.addFeature(evt, this.markerCurrentIcon, "curIconFeatureId");
    });
  },
  methods: {
    initMap() {
      // 获取地图容器
      this.mapDom = document.getElementById("map");
      // 初始化地图配置
      this.mapObj = new Map({
        target: this.mapDom, // 地图容器
        view: new View({
          center: [121.70185, 31.298777], // 地图中心点
          zoom: 15,
          minZoom: 14,
          maxZoom: 18,
          // EPSG: 3857 --web地图,基于球体的、web墨卡托投影(伪墨卡托投影Pseudo-Mercator)的投影坐标系,范围为纬度85度以下,由于google地图最先使用而成为事实标准。至今,大多互联网地图都使用EPSG3857,主要是因为该投影是等角投影,适合地图定向及导航,但是纬度越高,面积变形越大。
          // EPSG: 4326 --全球通用,基于WGS84椭球的经纬度坐标系,大地坐标系
          projection: "EPSG:4326", // 投影
          extent: this.extent,
        }),
      });

      // 添加一个使用离线瓦片地图的层
      const offlineMapLayer = new TileLayer({
        source: new XYZ({
          url: this.tileUrl,
        }),
      });
      // 将图层添加到地图
      this.mapObj.addLayer(offlineMapLayer);
    },
  // 加载地理坐标 -- 设置中心点
    handleSetMapCenter() {
      // 获取当前点击点、默认中心点的feature
      const curFeature =
        this.pointLayerSource.getFeatureById("curIconFeatureId");
      const centerFeature = this.pointLayerSource.getFeatureById(
        "centerIconFeatureId"
      );
      if (!curFeature || !centerFeature) {
        return;
      }
      // 首先移除curFeature
      this.pointLayerSource.removeFeature(curFeature);
      // 这里直接修改 原来中心点的坐标及样式
      centerFeature.getGeometry().setCoordinates(this.currentCoodinate);

      const newStyle = new Style({
        image: new Icon({
          color: "#ffffff",
          crossOrigin: "anonymous",
          src: this.markerCenterIcon,
        }),
      });
      centerFeature.setStyle(newStyle);
      // 修改原来中心点的坐标值
      this.centerPoint = {
        longitude: this.currentCoodinate[0],
        latitude: this.currentCoodinate[1]
      }
    },
    // 回到中心点
    handleBackMapCenter() {
      let view = this.mapObj.getView();
      view.setZoom(15);
      view.setCenter([this.centerPoint.longitude, this.centerPoint.latitude], "EPSG:4326", "EPSG:3857");
      this.mapObj.render();
    },
       // 地图点击事件
    addFeature(evt, src, id) {
      this.currentCoodinate = evt.coordinate;
      if (!Array.isArray(this.currentCoodinate)) {
        return;
      }
      const location = {
        longitude: this.currentCoodinate[0],
        latitude: this.currentCoodinate[1],
      };
      // 创建点击后的元素
      const point = new Feature({
        geometry: new Point([location.longitude, location.latitude]),
        data: location,
      });
      // 点的样式
      const iconStyle = new Style({
        image: new Icon({
          color: "#ffffff",
          crossOrigin: "anonymous",
          src: src,
        }),
      });
      // 设置样式
      point.setStyle(iconStyle);
      point.setId(id);
      this.addFeatureToLayer(point, "centerIconLayer");
    },
    // 在图层上新增元素
    addFeatureToLayer(feature, layerId) {
      const layer = this.getLayerById(layerId);
      const layerSource = layer.getSource();
      layerSource.addFeature(feature);
    },
    // 根据图层id来获取图层
    getLayerById(layerId) {
      const layers = this.mapObj.getLayers().getArray();
      for (let i = 0; i < layers.length; i++) {
        if (layers[i].get("id") === layerId) {
          return layers[i];
        }
      }
      return null;
    },
    // 添加地理坐标点
    addPoint(location, src, layerId, featureId) {
      // 地理坐标数组
      const pointData = [location];

      pointData.map((item) => {
        // 创建点
        const point = new Feature({
          geometry: new Point([item.longitude, item.latitude]),
          data: item,
        });
        // 点的样式
        const iconStyle = new Style({
          image: new Icon({
            color: "#ffffff",
            crossOrigin: "anonymous",
            src: src,
          }),
        });
        // 设置样式
        point.setStyle(iconStyle);
        point.setId(featureId);
        this.mapPointList.push(point);
      });

      // 创建geojson据源
      this.pointLayerSource = new VectorSource({ features: this.mapPointList });
      // 创建图层 并加载数据
      this.pointLayer = new VectorLayer({
        source: this.pointLayerSource,
        // 图层的id,这也非常重要
        id: layerId,
      });
      // 将图层添加地图上
      this.mapObj.addLayer(this.pointLayer);
    }, 
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/857359.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

2.4g无线芯片G350规格书详细介绍

G350是一款高度集成的2.4GHz无线收发芯片&#xff0c;旨在为各种应用提供低成本、高性能的无线通信解决方案。该芯片通过降低功耗&#xff0c;在保持寄存器值条件下&#xff0c;实现最低电流为5μA&#xff0c;从而显著提高了电池寿命。它内置了发射接收FIFO寄存器&#xff0c;…

【vue+el-table+el-backtop】表格结合返回顶部使用,loading局部加载

效果图: 一. 表格结合返回顶部 二. 局部loading 解决方法: 一 返回顶部 target绑定滚动dom的父元素类名就可以了. 1.如果你的表格是 固定表头 的,那滚动dom的父元素类名就是 el-table__body-wrapper <el-backtop target".el-table__body-wrapper" :visibility…

python练手项目百度网盘,python练手经典100例项目

大家好&#xff0c;小编来为大家解答以下问题&#xff0c;python练手项目 源代码 百度网盘&#xff0c;python练手项目码源百度网盘&#xff0c;现在让我们一起来看看吧&#xff01; 前言 Python 是一种面向对象、解释型、弱类型的脚本语言&#xff0c;它也是一种功能强大而完善…

班级事务管理系统设计与实现

班级事务管理系统 后端采用Spring Boot开发。 cloud分支版本正在开发中&#xff0c;后端采用Spring Cloud进行改造。 系统架构 项目采用B/S架构&#xff0c;前后端通讯采用RESTful API&#xff0c;数据格式使用Json&#xff0c;认证Token格式采用JWT。 身份认证使用Spring …

第五章 Opencv图像处理框架实战 5-8直方图与傅里叶变化

1、直方图定义 import cv2 #opencv读取的格式是BGR import numpy as np import matplotlib.pyplot as plt#Matplotlib是RGB %matplotlib inline def cv_show(img,name):cv2.imshow(name,img)cv2.waitKey()cv2.destroyAllWindows() 直方图 cv2.calcHist(images,channels,mask…

【音视频、chatGpt】h5页面最小化后,再激活后视频停住问题的解决

目录 现象 观察 解决 现象 页面有时候要切换&#xff0c;要最小化&#xff1b;短时间或者几个小时内切换回来&#xff0c;视频可以正常续上&#xff1b;而放置较长时间&#xff0c;几个小时或者一晚上&#xff0c;切换回来后&#xff0c;视频可能卡死 观察 切换页面&#x…

如何给Linux开启swap虚拟内存

查看系统内存资源 free -h 创建swap分区 dd if/dev/zero of/swapfile bs1024 count4194304dev/zero&#xff1a;是Linux的一种特殊字符设备(输入设备)&#xff0c;可以用来创建一个指定长度用于初始化的空文件&#xff0c;如临时交换文件&#xff0c;该设备无穷尽地提供0&…

工程监测仪器振弦传感器信号转换器应用于隧洞监测

工程监测仪器振弦传感器信号转换器应用于隧洞监测 隧洞建设是重大工程项目&#xff0c;监测隧洞结构和环境的变化对确保隧洞安全和运行管理至关重要。工程监测仪器是实现隧洞监测的关键设备&#xff0c;其中振弦传感器和信号转换器是非常重要的组成部分。 振弦传感器是一种专门…

AI自动驾驶

AI自动驾驶 一、自动驾驶的原理二、自动驾驶的分类三、自动驾驶的挑战四、自动驾驶的前景五、关键技术六、自动驾驶的安全问题七、AI数据与自动驾驶八、自动驾驶的AI算法总结 自动驾驶技术是近年来备受关注的热门话题。它代表了人工智能和机器学习在汽车行业的重要应用。本文将…

SAP Range 表

Range表装的一些个复杂的可以选择的值。有时候单选的值不够用的&#xff0c;用Range表。 数据结构就是Select option一样的。当你在选择屏幕定义一个selection-option的时候&#xff0c;系统自动定义个range表。那我们自己想定义个来用用咋搞&#xff1f; Range表有四列&…

在rviz中实时显示车辆轨迹

在工作空间中创建包 cd ~/catkin_ws/src catkin_create_pkg trajectory_display_example roscpp nav_msgs sensor_msgs在src文件夹下创建一个C源文件 #include <ros/ros.h> #include <nav_msgs/Odometry.h> #include <nav_msgs/Path.h> #include <senso…

roi感兴趣区域像素值统计,求roi感兴趣区域内像素值的最小值、最大值、均值、标准差(标准方差)

文章目录 1、求roi感兴趣区域内像素值的最小值、最大值minMaxLoc() 函数原型&#xff1a;&#xff08;1&#xff09;原型一&#xff1a;&#xff08;2&#xff09;原型二&#xff1a;&#xff08;3&#xff09;另外与 minMaxLoc()函数原型一&#xff0c;用法相同的函数 minMaxI…

mac ssh连接另一台window虚拟机vm

vmware配置端口映射 编辑(E) > 虚拟网络编辑器(N)... > NAT设置(S)... window防火墙&#xff0c;入站规则添加5555端口 控制面板 > 系统和安全 > Windows 防火墙>高级设置>入站规则>新建规则... tips windows查看端口命令&#xff1a;netstat -ano | f…

html实现商品图片放大镜,html图片放大镜预览

效果 实现 复制粘贴&#xff0c;修改图片路径即可使用 <!DOCTYPE html> <html><head><meta charset"UTF-8"><title>商品图片放大镜</title></head><style>body {margin: 0;padding: 0;}#app {padding: 10px;posit…

基于Java+SpringBoot+Vue的网吧管理系统设计与实现(源码+LW+部署文档等)

博主介绍&#xff1a; 大家好&#xff0c;我是一名在Java圈混迹十余年的程序员&#xff0c;精通Java编程语言&#xff0c;同时也熟练掌握微信小程序、Python和Android等技术&#xff0c;能够为大家提供全方位的技术支持和交流。 我擅长在JavaWeb、SSH、SSM、SpringBoot等框架…

CEC2013(MATLAB):遗传算法(Genetic Algorithm,GA)求解CEC2013的28个函数

一、遗传算法GA 遗传算法&#xff08;Genetic Algorithm&#xff0c;GA&#xff09;起源于对生物系统所进行的计算机模拟研究&#xff0c;是一种随机全局搜索优化方法&#xff0c;它模拟了自然选择和遗传中发生的复制、交叉(crossover)和变异(mutation)等现象&#xff0c;从任…

国际货币汇率 API 数据接口

国际货币汇率 API 数据接口 支持多种货币对&#xff0c;每日精准汇率&#xff0c;多币种支持。 1. 产品功能 支持多种货币汇率查询&#xff1b;支持部分加密货币汇率查询&#xff1b;数据为每日更新汇率数据&#xff1b;可一次查询源货币代码对应所有目标货币汇率&#xff1b…

无涯教程-Perl - getppid函数

描述 该函数返回父进程的进程ID。 语法 以下是此函数的简单语法- getppid返回值 该函数返回父进程的进程ID。 例 以下是显示其基本用法的示例代码- #!/usr/bin/perl$ppidgetppid();print "Parent Process ID $ppid\n";执行上述代码后,将产生以下输出- Paren…

多模态模型评价

论文1 【Evaluating Object Hallucination in Large Vision-Language Models】 这篇文章主要是评价视觉-语言模型中出现“幻觉”的评价。论文中是这样定义幻觉的 we find that LVLMs suffer from the hallucination problem, i.e., they tend to generate objects that are in…

哪个类包含clone方法?是Cloneable还是Object?

在Java中&#xff0c;clone方法是定义在Object类中的。所有的Java类都继承自Object类&#xff0c;因此每个Java对象都继承了clone方法。然而&#xff0c;要成功地使用clone方法&#xff0c;需要满足一些条件&#xff0c;其中之一是被克隆的类必须实现Cloneable接口。 虽然clone…