openlayers6的引入TileWMS服务的图层,(直接拷贝就能用)

博客介绍了在Vue项目中使用OpenLayers6的情况,成功引入后端服务,且该服务可100%正常使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


<template>
  <div>
    <div id="mapDiv" class="map"></div>
    <div class="mouse-position-container">
      <span>经度: {{ jingdu }}, 纬度: {{ weidu }}</span>
    </div>
  </div>
</template>

<script>
import {Map, View,control} from 'ol';
// import {control} from 'ol/control';
import TileLayer from 'ol/layer/Tile';
import TileWMS from 'ol/source/TileWMS';
import {MousePosition} from 'ol/control';
import {fromLonLat} from 'ol/proj';

import 'ol/ol.css';

export default {
  name: 'MyMap',
  data() {
    return {
      map: null,
      tileWMSSource: null,
      jingdu: '',
      weidu: '',
    };
  },
  mounted() {
    this.initMap();
  },
  methods: {
    initMap() {

      // 初始化地图
      //获取到当前的路径
      // process.env.VUE_APP_BASE_SERVER+
      const baseURL = process.env.VUE_APP_BASE_SERVERMap
      this.tileWMSSource = new TileWMS({
        url:baseURL+'/geoserver/rastertool/wms?service=WMS&version=1.1.0&request=GetMap&layers=rastertool%3Aregion3857&bbox=1.2848143850114737E7%2C4785205.69244395%2C1.3080951909046063E7%2C5021077.556513484&width=758&height=768&srs=EPSG%3A3857&styles=&format=application/openlayers',
        // params: {
        //   LAYERS: 'rastertool:region3857',
        //   TILED: true,
        //   STYLES: 'wfds_style_GB2312',
        // },
        // serverType: 'geoserver',
        // projection: 'EPSG:3857',
      });
      const tileLayer = new TileLayer({
        source: this.tileWMSSource,
      });
      this.map = new Map({
        layers: [tileLayer],
        target: 'mapDiv',
        view: new View({
          center: fromLonLat([115.41682666500014, 39.44207842500009]),
          zoom: 9,
          projection: 'EPSG:3857',
        }),


      });

      // 监听地图点击事件
      this.map.on('click', (event) => {
        const coords = fromLonLat(event.coordinate);
        this.jingdu = coords[0].toFixed(6);
        this.weidu = coords[1].toFixed(6);
      });
    },
  },
};
</script>

<style scoped>
.map {
  height: 500px;
}
.mouse-position-container {
  position: absolute;
  bottom: 20px;
  left: 20px;
  background-color: rgba(255, 255, 255, 0.5);
  padding: 5px;
  border-radius: 5px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
}
.mouse-position {
  font-size: 14px;
}
</style>


在这里插入图片描述
使用的是openlayers6,
效果是引入了后端发的服务,100%可以使用,并且是在vue项目中使用的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值