国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

vue+openlayers,初始化openlayers地圖,實現(xiàn)鼠標移入、點擊、右鍵等事件

這篇具有很好參考價值的文章主要介紹了vue+openlayers,初始化openlayers地圖,實現(xiàn)鼠標移入、點擊、右鍵等事件。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

主要功能:初始化openlayers地圖,實現(xiàn)鼠標移入、點擊、右鍵等事件,以及獲取當前圖標的feature,將當前圖標信息以彈框方式進行展示;地圖上展示拾取到的經(jīng)緯度
前端使用的是vue技術(shù)棧
步驟一:將地圖的公用配置項單獨提出成一個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我提取到了公用全局變量里,內(nèi)容和上邊是一樣的
          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地圖文章來源地址http://www.zghlxwxcb.cn/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里內(nèi)容
            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);
        //設(shè)置地圖縮放監(jiān)聽
        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出去,方便調(diào)用組件的頁面單獨使用
        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>

到了這里,關(guān)于vue+openlayers,初始化openlayers地圖,實現(xiàn)鼠標移入、點擊、右鍵等事件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • Vue+OpenLayers 創(chuàng)建地圖并顯示鼠標所在經(jīng)緯度

    Vue+OpenLayers 創(chuàng)建地圖并顯示鼠標所在經(jīng)緯度

    本文用的是高德地圖 頁面 初始化地圖 附css代碼

    2024年01月17日
    瀏覽(29)
  • Vue 新版 腳手架 初始化 筆記

    Vue2/Vue3 修改 node 更新源 將默認的 更新源修改為 淘寶的 下載地址 安裝 一般 新版 Vue 腳手架不可以共存 所以如果有 舊版的腳手架 會提示你 需要卸載 原來的腳手架 然后重新執(zhí)行上面的安裝 安裝好之后 就可以去初始化項目了 值得一提的是我在官方的文檔中找到了一個 在使

    2024年02月20日
    瀏覽(45)
  • 1、前端項目初始化(vue3)

    1、前端項目初始化(vue3)

    安裝npm,(可以用nvm管理npm版本)npm安裝需要安裝node.js(綁定銷售?)而使用nvm就可以很方便的下載不同版本的node,這里是常用命令 配置npm源 命令: 設(shè)置鏡像源: npm config set registry https://registry.npm.taobao.org 查看當前使用的鏡像地址: npm config get registry 參考 :https://www.cnbl

    2024年01月20日
    瀏覽(26)
  • 初始化vue中data中的數(shù)據(jù)

    初始化vue中data中的數(shù)據(jù)

    當組件的根元素使用了v-if的時候, 并不會初始化data中的數(shù)據(jù) 如果想完全銷毀該組件并且初始化數(shù)據(jù),需要在使用該組件的本身添加v-if 或者是手動初始化該組件中的數(shù)據(jù) 下面詳細說說Object.assign的用法: ES6的官方文檔的解釋是:Object.assign() 方法用于將所有可枚舉屬性的值從一

    2024年02月05日
    瀏覽(18)
  • Vue3+Vite 初始化Cesium

    git

    2024年02月11日
    瀏覽(16)
  • openlayers實現(xiàn)鎖定地圖,不可使用鼠標拖動放大縮小地圖

    openlayers實現(xiàn)鎖定地圖,不可使用鼠標拖動放大縮小地圖

    開啟地圖鎖定功能,不再允許使用鼠標拖拽查看地圖,使用鼠標滾輪放大縮小查看地圖 關(guān)鍵代碼 包含業(yè)務開關(guān)的代碼 注:這個圖是別的大佬整理的 https://www.ktanx.com/blog/p/2656

    2024年02月02日
    瀏覽(31)
  • 構(gòu)建vue初始化項目:vue create 命令構(gòu)建vue項目

    構(gòu)建vue初始化項目:vue create 命令構(gòu)建vue項目

    首先找到自己的文件夾 1.創(chuàng)建vue項目: vue create vue 2.選擇Manually select features自定義創(chuàng)建 3.選擇vue版本(這里選擇的是vue2) 4. 5. 6. 7. 8創(chuàng)建完成 創(chuàng)建完項目后先刪除node_modules然后執(zhí)行 npm設(shè)置淘寶鏡像加速:npm config set registry https://registry.npm.taobao.org 然后再執(zhí)行 npm安裝: npm install

    2024年02月08日
    瀏覽(24)
  • 243:vue+Openlayers 更改鼠標滾輪縮放地圖大小,每次縮放小一點

    243:vue+Openlayers 更改鼠標滾輪縮放地圖大小,每次縮放小一點

    第243個 點擊查看專欄目錄 本示例的目的是介紹如何在vue+openlayers項目中設(shè)置鼠標滾輪縮放地圖大小,每次滑動一格滾輪,設(shè)定的值非默認值1。具體的設(shè)置方法,參考源代碼。 直接復制下面的 vue+openlayers源代碼,操作2分鐘即可運行實現(xiàn)效果 示例效果

    2024年02月09日
    瀏覽(117)
  • 【vue3項目初始化配置】vue3 + element plus

    【vue3項目初始化配置】vue3 + element plus

    項目初始化是開發(fā)過程中很重要的一個環(huán)節(jié),本篇博客帶大家從零開始創(chuàng)建并初始化一個vue3項目,文章詳細介紹了每個步驟,希望能幫助剛接觸開發(fā)的小伙伴。 目錄 一.創(chuàng)建項目 二.安裝插件 ??編輯 ?編輯三.安裝依賴 ?編輯??編輯四.配置項目 配置vu.config.js文件 ?配置

    2024年01月18日
    瀏覽(23)
  • Vue2電商前臺項目——項目的初始化及搭建

    Vue2電商前臺項目——項目的初始化及搭建

    Vue基礎(chǔ)知識點擊此處——Vue.js 使用腳手架創(chuàng)建項目,在需要放置項目的目錄下打開cmd輸入: 這個name是項目名(我的項目名是potato-mall 創(chuàng)建有問題或者不太熟悉的具體參考之前的腳手架配置筆記 1、腳手架目錄介紹 項目創(chuàng)建成功后,點開項目目錄,會出現(xiàn)以下文件: 這些文件

    2024年02月09日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包