vue+openlayers,初始化openlayers地图,实现鼠标移入、点击、右键等事件

这篇具有很好参考价值的文章主要介绍了vue+openlayers,初始化openlayers地图,实现鼠标移入、点击、右键等事件。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

主要功能:初始化openlayers地图,实现鼠标移入、点击、右键等事件,以及获取当前图标的feature,将当前图标信息以弹框方式进行展示;地图上展示拾取到的经纬度
前端使用的是vue技术栈
步骤一:将地图的公用配置项单独提出成一个js文件,方便打包后进行修改,代码如下

import TileLayer from 'ol/layer/Tile'
import TileArcGISRest from 'ol/source/TileArcGISRest'
import OSM from 'ol/source/OSM'
import XYZ from 'ol/source/XYZ'
//0表示部署的离线瓦片地图,1表示OSM,2表示使用Arcgis在线午夜蓝地图服务
let streetMap=function(){
  let mapLayer = null
  switch(mapType){
    case 0:
      mapLayer=new TileLayer({
        source: new XYZ({
          // url:'http://你的地图服务器地址:8080/geowebcache/service/wmts?tilematrix=EPSG%3A3857_google_rs%3A{z}&layer=google_rs&style=raster&tilerow={y}&tilecol={x}&tilematrixset=EPSG%3A3857_google_rs&format=image%2Fjpeg&service=WMTS&version=1.0.0&request=GetTile'
          //mapUrl我提取到了公用全局变量里,内容和上边是一样的
          url: mapUrl
        })
      })
      break
    case 1:
      mapLayer=new TileLayer({
        source: new OSM()
      })
      break
    case 2:
      mapLayer=new TileLayer({
        source:new TileArcGISRest({
          url:'https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer'
        })
      })
      break
  }
  return [mapLayer]
}

let mapConfig = {
  x: 115.8906,
  y: 19.1597,
  zoom: 6,//初始地图级别
  streetMap: streetMap
}

export default mapConfig

步骤二:初始化openlayers地图文章来源地址https://www.toymoban.com/news/detail-505244.html

<template>
  <div class="map-view-wrapper">
    <div class='olMap' ref='rootMap'></div>
    <div id="mouse-position"></div>
    <div id="popup" class="ol-popup">
      <div class="ol-popup-title">
        <span>{{popupText}}</span>
      </div>
    </div>
  </div>
</template>
<script>
  import 'ol/ol.css'
  import { Map, View } from 'ol'
  import {ScaleLine} from 'ol/control';
  import MousePosition from 'ol/control/MousePosition';
  import {createStringXY} from 'ol/coordinate';
  import {defaults as defaultControls} from 'ol/control';
  import mapConfig from "../../../config/mapconfig";
  import Overlay from "ol/Overlay";
  import {Icon, Style} from "ol/style";
  export default {
    name: 'MapView',
    components:{},
    props:{
    },
    watch:{
    },
    data () {
      return {
        map: null,
        overlay:null,
        popupCoordinates: "",
        popupText: "",
        moveFeature:null
      }
    },
    mounted () {
      this.initMap();
    },
    methods: {
      initMap(){
        let mousePositionControl = new MousePosition({
          coordinateFormat: createStringXY(4),
          projection: 'EPSG:4326',
          className: 'custom-mouse-position',
          target: document.getElementById('mouse-position'),
          undefinedHTML: '&nbsp;',
        });
        let mapContainer = this.$refs.rootMap;
        this.map = new Map({
          target: mapContainer,
          controls: defaultControls().extend([mousePositionControl]),
          layers: mapConfig.streetMap(),
          view: new View({
            projection: 'EPSG:4326',
            //center: [113.960623, 22.546082],
            center: [mapConfig.x, mapConfig.y],//读取上边mapConfig.js里内容
            zoom: mapConfig.zoom,
            // minZoom:4,
            maxZoom: 6,
          })
        })
        //添加比例尺 //单位有5种:degrees imperial us nautical metric
        this.map.addControl(new ScaleLine({units: 'metric'}));
        let me = this;
        //鼠标移入点是,展示的弹框
        this.overlay = new Overlay({
          element: document.getElementById('popup'),
          autoPan: true,
          autoPanAnimation: {
            duration: 250
          }
        });
        this.map.addOverlay(this.overlay);
        //设置地图缩放监听
        this.map.on("moveend", function (e) {
          let zoom = me.map.getView().getZoom();
          // console.log("zoom",zoom);
        });
        //鼠标移动
        this.map.on('pointermove',function (e){
          let feature = me.map.forEachFeatureAtPixel(e.pixel,function (feature,layer){
            return feature;
          })
          if(feature){
            if(feature.get("type")){
              let types = feature.get("type").split("&");
              if(!me.isEmpty(types[1])){
                me.popupText = types[1];
                me.popupCoordinates = feature.getGeometry().flatCoordinates;
                // me.overlay.setPosition(me.popupCoordinates);
                me.overlay.setPosition([parseFloat(me.popupCoordinates[0])+0.45,parseFloat(me.popupCoordinates[1])]);
              }
            }
            me.map.getTargetElement().style.cursor="pointer";
          }else{
            //me.moveFeature && me.moveFeature.setStyle(me.setMapMoveStyle('out'));
            me.map.getTargetElement().style.cursor="auto";
            me.popupCoordinates && me.overlay.setPosition(undefined);
            me.popupCoordinates = null;
          }
        })
        //地图点击
        me.map.on('click',function (e){
          let feature = me.map.forEachFeatureAtPixel(e.pixel,function (feature,layer){
            return feature;
          })
          if(feature){
            me.$emit('click-icon',feature)
          }
        })
        //右键事件
        me.map.on('contextmenu',function (e){
          e.preventDefault();
          let feature = me.map.forEachFeatureAtPixel(e.pixel,function (feature,layer){
            return feature;
          })
          if(feature){
            // console.log("右击地图feature",feature);
            me.moveFeature = feature;
            feature.setStyle(me.setMapMoveStyle('in'));
            //抛出右击点击
            me.$emit('map-contextmenu',feature);
          }
        })
        //因为我将地图单独封装成了组件,所以这里将map emit出去,方便调用组件的页面单独使用
        setTimeout(function () {
          me.$emit('out-map',me.map)
        },100)
      },
      //修改鼠标移入、移出的样式
      setMapMoveStyle(str){
        let src = str === 'in' ? "./static/images/port-icon-select.png" : "./static/images/port-icon-new.png";
        let iconStyle = new Style({
          image: new Icon({
            anchor: [0.5, 0.5],
            anchorXUnits: 'fraction',
            anchorYUnits: 'fraction',
            src: src,
          })
        });
        return iconStyle;
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "../../../assets/styles/base_style";
  .map-view-wrapper{
    width:100%;
    height: 100%;
    position: relative;
    .olMap{
      width:100%;
      height: calc(100%);
    }
    .ol-popup{
      background: rgba(32,100,176,.8);
      color: #fff;
      font-size: 14px;
      padding: 2px 8px;
      border: 1px solid #22CEFF;
      border-radius: 6px;
    }
    #mouse-position{
      position: absolute;
      bottom: 5px;
      right:20px;
    }
  }
</style>

到了这里,关于vue+openlayers,初始化openlayers地图,实现鼠标移入、点击、右键等事件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Vue+OpenLayers 创建地图并显示鼠标所在经纬度

    本文用的是高德地图 页面 初始化地图 附css代码

    2024年01月17日
    浏览(37)
  • 1、前端项目初始化(vue3)

    安装npm,(可以用nvm管理npm版本)npm安装需要安装node.js(绑定销售?)而使用nvm就可以很方便的下载不同版本的node,这里是常用命令 配置npm源 命令: 设置镜像源: npm config set registry https://registry.npm.taobao.org 查看当前使用的镜像地址: npm config get registry 参考 :https://www.cnbl

    2024年01月20日
    浏览(43)
  • Vue 新版 脚手架 初始化 笔记

    Vue2/Vue3 修改 node 更新源 将默认的 更新源修改为 淘宝的 下载地址 安装 一般 新版 Vue 脚手架不可以共存 所以如果有 旧版的脚手架 会提示你 需要卸载 原来的脚手架 然后重新执行上面的安装 安装好之后 就可以去初始化项目了 值得一提的是我在官方的文档中找到了一个 在使

    2024年02月20日
    浏览(27)
  • Vue3+Vite 初始化Cesium

    git

    2024年02月11日
    浏览(29)
  • 初始化vue中data中的数据

    当组件的根元素使用了v-if的时候, 并不会初始化data中的数据 如果想完全销毁该组件并且初始化数据,需要在使用该组件的本身添加v-if 或者是手动初始化该组件中的数据 下面详细说说Object.assign的用法: ES6的官方文档的解释是:Object.assign() 方法用于将所有可枚举属性的值从一

    2024年02月05日
    浏览(25)
  • openlayers实现锁定地图,不可使用鼠标拖动放大缩小地图

    开启地图锁定功能,不再允许使用鼠标拖拽查看地图,使用鼠标滚轮放大缩小查看地图 关键代码 包含业务开关的代码 注:这个图是别的大佬整理的 https://www.ktanx.com/blog/p/2656

    2024年02月02日
    浏览(38)
  • 构建vue初始化项目:vue create 命令构建vue项目

    首先找到自己的文件夹 1.创建vue项目: vue create vue 2.选择Manually select features自定义创建 3.选择vue版本(这里选择的是vue2) 4. 5. 6. 7. 8创建完成 创建完项目后先删除node_modules然后执行 npm设置淘宝镜像加速:npm config set registry https://registry.npm.taobao.org 然后再执行 npm安装: npm install

    2024年02月08日
    浏览(40)
  • 243:vue+Openlayers 更改鼠标滚轮缩放地图大小,每次缩放小一点

    第243个 点击查看专栏目录 本示例的目的是介绍如何在vue+openlayers项目中设置鼠标滚轮缩放地图大小,每次滑动一格滚轮,设定的值非默认值1。具体的设置方法,参考源代码。 直接复制下面的 vue+openlayers源代码,操作2分钟即可运行实现效果 示例效果

    2024年02月09日
    浏览(61)
  • 【vue3项目初始化配置】vue3 + element plus

    项目初始化是开发过程中很重要的一个环节,本篇博客带大家从零开始创建并初始化一个vue3项目,文章详细介绍了每个步骤,希望能帮助刚接触开发的小伙伴。 目录 一.创建项目 二.安装插件  ​编辑 ​编辑三.安装依赖 ​编辑 ​编辑四.配置项目 配置vu.config.js文件  配置

    2024年01月18日
    浏览(61)
  • Vue2电商前台项目——项目的初始化及搭建

    Vue基础知识点击此处——Vue.js 使用脚手架创建项目,在需要放置项目的目录下打开cmd输入: 这个name是项目名(我的项目名是potato-mall 创建有问题或者不太熟悉的具体参考之前的脚手架配置笔记 1、脚手架目录介绍 项目创建成功后,点开项目目录,会出现以下文件: 这些文件

    2024年02月09日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包