Vue+Openlayers實現地圖縮放圖標等比例縮放


場景

Vue+Openlayers實現顯示圖片並分優先級多圖層加載:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/121235987

上面實現的效果如下

 

 

 

 

如果要實現地圖縮放時圖標等比例放大縮小,效果如下

 

 

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

1、首先在頁面加載完之后,設置定時器進行一秒一次的渲染燈圖層的數據。

 

    this.initMap();
    setInterval(() => {
      this.initLightData();
    }, 1000)
  },

 

這里是使用定時器的方式一秒執行一次的渲染燈圖層的數據。

在真實業務中,這里可能是由后台推送的比如紅綠燈的數據,然后進行定時的渲染燈圖層的數據。

比如后台獲取地圖上所有紅綠的信號,然后定時推送給前端,前端在收到數據后重新渲染燈的圖層。

2、然后在渲染燈圖層的方法中,獲取當前地圖的縮放等級,乘以一個比例,賦值給Style的image的

scale屬性。

    initLightData(){
      this.lightLayer.getSource().clear();
      this.lightData.forEach((item, index) => {
          var feature = new Feature({
              geometry: new Point([Number(item.x), Number(item.y)]),
          });
          let url = "images/light.png";
          const zoom = this.map.getView().getZoom();
          let style = new Style({
                  image: new Icon({
                      scale: 0.15 * (zoom -13) ,
                      src: url,
                      anchor: [0.48, 0.52],
                  }),
              });
          feature.setStyle(style);
          this.lightLayer.getSource().addFeature(feature);
      });
    },

這里的scale的比例算法根據實際要的效果進行調整,這里的0.15是自己所需要的比例。

3、完整代碼

<template>
  <div id="map" class="map"></div>
</template>

<script>
//導入基本模塊
import "ol/ol.css";
import Map from "ol/Map";
import View from "ol/View";
import { Point } from "ol/geom";
import Feature from "ol/Feature";
import { Icon,Style} from "ol/style";
//導入相關模塊
import { Tile as TileLayer , Vector as VectorLayer } from 'ol/layer'
import { TileWMS ,Vector as VectorSource } from 'ol/source'
export default {
  name: "olMapImageWMSMulLayers",
  data() {
    return {
      map: null, // map地圖
      layer:null, //地圖圖層
      lightLayer:null, //燈圖層
      houseLayer:null, //房子圖層
      //紅綠燈數據
      lightData:[
        {x:"987798.93778", y:"213885.81024"},
        {x:"987710.93778", y:"213810.81024"},
      ],
      //房子數據
      houseData:[
        {x:"986610.93778", y:"213885.81024"},
        {x:"986510.93778", y:"213810.81024"},
      ],
   };
  },
  mounted() {
    this.initMap();
    setInterval(() => {
      this.initLightData();
    }, 1000)
  },
  methods: {

    //初始化紅綠燈數據
    initLightData(){
      this.lightLayer.getSource().clear();
      this.lightData.forEach((item, index) => {
          var feature = new Feature({
              geometry: new Point([Number(item.x), Number(item.y)]),
          });
          let url = "images/light.png";
          const zoom = this.map.getView().getZoom();
          let style = new Style({
                  image: new Icon({
                      scale: 0.15 * (zoom -13) ,
                      src: url,
                      anchor: [0.48, 0.52],
                  }),
              });
          feature.setStyle(style);
          this.lightLayer.getSource().addFeature(feature);
      });
    },

    //初始化房子數據
    initHouseData(){
      this.houseLayer.getSource().clear();
      this.houseData.forEach((item, index) => {
          var feature = new Feature({
              geometry: new Point([Number(item.x), Number(item.y)]),
          });
          let url = "images/house.png";
          let style = new Style({
                  image: new Icon({
                      scale: 0.3,
                      src: url,
                      anchor: [0.48, 0.52],
                  }),
              });
          feature.setStyle(style);
          this.houseLayer.getSource().addFeature(feature);
      });
    },

    initMap() {
      //地圖圖層
      this.layer = new TileLayer({
        source: new TileWMS({
          //不能設置為0,否則地圖不展示。
          ratio: 1,
          url: "http://localhost:8000/geoserver/nyc/wms",
          params: {
            LAYERS: "nyc:nyc_roads",
            STYLES: "",
            VERSION: "1.1.1",
            tiled: true
          },
          serverType: "geoserver",
        }),
      });

      // 紅綠燈的圖層
      this.lightLayer = new VectorLayer({
          source: new VectorSource(),
      });
     
      //房子的圖層
      this.houseLayer = new VectorLayer({
          source: new VectorSource(),
      });

      this.map = new Map({
        //地圖容器ID
        target: "map",
        //引入地圖
        layers: [this.layer,this.lightLayer,this.houseLayer],
        view: new View({
          //地圖中心點
          center: [987777.93778, 213834.81024],
          zoom: 12,
          minZoom:6, // 地圖縮放最小級別
          maxZoom:19,
        }),
      });

      this.initLightData();
      this.initHouseData();
      //獲取點的監聽方法設置
      this.onPoint()
    },
    //  獲取點
    onPoint() {
      // 監聽singleclick事件
      this.map.on('singleclick', function(e) {
        console.log(e.coordinate)
      })
    }
  },
};
</script>

<style scoped>
.map {
  width: 100%;
  height: 800px;
}
</style>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM