当前位置:   article > 正文

Vue+Openlayers实现地图缩放图标等比例缩放_js vue 大图片像地图一样分片加载,还能缩放

js vue 大图片像地图一样分片加载,还能缩放

场景

Vue+Openlayers实现显示图片并分优先级多图层加载:

Vue+Openlayers实现显示图片并分优先级多图层加载_BADAO_LIUMANG_QIZHI的博客-CSDN博客

上面实现的效果如下

如果要实现地图缩放时图标等比例放大缩小,效果如下

注:

博客:
BADAO_LIUMANG_QIZHI的博客_霸道流氓气质_CSDN博客
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

1、首先在页面加载完之后,设置定时器进行一秒一次的渲染灯图层的数据。

  1.     this.initMap();
  2.     setInterval(() => {
  3.       this.initLightData();
  4.     }, 1000)
  5.   },

这里是使用定时器的方式一秒执行一次的渲染灯图层的数据。

在真实业务中,这里可能是由后台推送的比如红绿灯的数据,然后进行定时的渲染灯图层的数据。

比如后台获取地图上所有红绿的信号,然后定时推送给前端,前端在收到数据后重新渲染灯的图层。

2、然后在渲染灯图层的方法中,获取当前地图的缩放等级,乘以一个比例,赋值给Style的image的

scale属性。

  1.     initLightData(){
  2.       this.lightLayer.getSource().clear();
  3.       this.lightData.forEach((item, index) => {
  4.           var feature = new Feature({
  5.               geometry: new Point([Number(item.x), Number(item.y)]),
  6.           });
  7.           let url = "images/light.png";
  8.           const zoom = this.map.getView().getZoom();
  9.           let style = new Style({
  10.                   image: new Icon({
  11.                       scale: 0.15 * (zoom -13) ,
  12.                       src: url,
  13.                       anchor: [0.48, 0.52],
  14.                   }),
  15.               });
  16.           feature.setStyle(style);
  17.           this.lightLayer.getSource().addFeature(feature);
  18.       });
  19.     },

这里的scale的比例算法根据实际要的效果进行调整,这里的0.15是自己所需要的比例。

3、完整代码

  1. <template>
  2.   <div id="map" class="map"></div>
  3. </template>
  4. <script>
  5. //导入基本模块
  6. import "ol/ol.css";
  7. import Map from "ol/Map";
  8. import View from "ol/View";
  9. import { Point } from "ol/geom";
  10. import Feature from "ol/Feature";
  11. import { Icon,Style} from "ol/style";
  12. //导入相关模块
  13. import { Tile as TileLayer , Vector as VectorLayer } from 'ol/layer'
  14. import { TileWMS ,Vector as VectorSource } from 'ol/source'
  15. export default {
  16.   name: "olMapImageWMSMulLayers",
  17.   data() {
  18.     return {
  19.       map: null, // map地图
  20.       layer:null, //地图图层
  21.       lightLayer:null, //灯图层
  22.       houseLayer:null, //房子图层
  23.       //红绿灯数据
  24.       lightData:[
  25.         {x:"987798.93778", y:"213885.81024"},
  26.         {x:"987710.93778", y:"213810.81024"},
  27.       ],
  28.       //房子数据
  29.       houseData:[
  30.         {x:"986610.93778", y:"213885.81024"},
  31.         {x:"986510.93778", y:"213810.81024"},
  32.       ],
  33.    };
  34.   },
  35.   mounted() {
  36.     this.initMap();
  37.     setInterval(() => {
  38.       this.initLightData();
  39.     }, 1000)
  40.   },
  41.   methods: {
  42.     //初始化红绿灯数据
  43.     initLightData(){
  44.       this.lightLayer.getSource().clear();
  45.       this.lightData.forEach((item, index) => {
  46.           var feature = new Feature({
  47.               geometry: new Point([Number(item.x), Number(item.y)]),
  48.           });
  49.           let url = "images/light.png";
  50.           const zoom = this.map.getView().getZoom();
  51.           let style = new Style({
  52.                   image: new Icon({
  53.                       scale: 0.15 * (zoom -13) ,
  54.                       src: url,
  55.                       anchor: [0.48, 0.52],
  56.                   }),
  57.               });
  58.           feature.setStyle(style);
  59.           this.lightLayer.getSource().addFeature(feature);
  60.       });
  61.     },
  62.     //初始化房子数据
  63.     initHouseData(){
  64.       this.houseLayer.getSource().clear();
  65.       this.houseData.forEach((item, index) => {
  66.           var feature = new Feature({
  67.               geometry: new Point([Number(item.x), Number(item.y)]),
  68.           });
  69.           let url = "images/house.png";
  70.           let style = new Style({
  71.                   image: new Icon({
  72.                       scale: 0.3,
  73.                       src: url,
  74.                       anchor: [0.48, 0.52],
  75.                   }),
  76.               });
  77.           feature.setStyle(style);
  78.           this.houseLayer.getSource().addFeature(feature);
  79.       });
  80.     },
  81.     initMap() {
  82.       //地图图层
  83.       this.layer = new TileLayer({
  84.         source: new TileWMS({
  85.           //不能设置为0,否则地图不展示。
  86.           ratio: 1,
  87.           url: "http://localhost:8000/geoserver/nyc/wms",
  88.           params: {
  89.             LAYERS: "nyc:nyc_roads",
  90.             STYLES: "",
  91.             VERSION: "1.1.1",
  92.             tiled: true
  93.           },
  94.           serverType: "geoserver",
  95.         }),
  96.       });
  97.       // 红绿灯的图层
  98.       this.lightLayer = new VectorLayer({
  99.           source: new VectorSource(),
  100.       });
  101.      
  102.       //房子的图层
  103.       this.houseLayer = new VectorLayer({
  104.           source: new VectorSource(),
  105.       });
  106.       this.map = new Map({
  107.         //地图容器ID
  108.         target: "map",
  109.         //引入地图
  110.         layers: [this.layer,this.lightLayer,this.houseLayer],
  111.         view: new View({
  112.           //地图中心点
  113.           center: [987777.93778, 213834.81024],
  114.           zoom: 12,
  115.           minZoom:6, // 地图缩放最小级别
  116.           maxZoom:19,
  117.         }),
  118.       });
  119.       this.initLightData();
  120.       this.initHouseData();
  121.       //获取点的监听方法设置
  122.       this.onPoint()
  123.     },
  124.     //  获取点
  125.     onPoint() {
  126.       // 监听singleclick事件
  127.       this.map.on('singleclick', function(e) {
  128.         console.log(e.coordinate)
  129.       })
  130.     }
  131.   },
  132. };
  133. </script>
  134. <style scoped>
  135. .map {
  136.   width: 100%;
  137.   height: 800px;
  138. }
  139. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/110005
推荐阅读
  

闽ICP备14008679号