vue+openlayers设置地图背景颜色(本例子为背景颜色设置为黑色)

在Vue项目中使用OpenLayers时,若要将地图背景颜色设为黑色,无需直接在OpenLayers中操作,只需修改包含地图的div元素的背景色。通过CSS设置该div的背景颜色即可实现。

思考:我之前用的是JavaScripts+openlayers,在网上搜了很多资料,自己也尝试了很多次,都没有设置成功,貌似不可行,估计需要拿过来的地图就是黑色的,这个可以去找黑色地图(我没找啊,因为后来需求变了,需要将openlayers集成到vue中,于是我就改变策略了),vue+openlayers设置背景颜色其实很简单,只需要设置地图所在的div的背景颜色即可

代码:

<template>
    <div id="map" class="mapStyle" ref="rootmap">
       
    </div>
</template>

<script>
import "ol/ol.css";
import { Map, View } from "ol";
// import TileLayer from "ol/layer/Tile";

import VectorLayer from "ol/layer/Vector";

// import OSM from "ol/source/OSM";
import VectorSource from "ol/source/Vector";
import Feature from "ol/Feature";
import GeoJSON from "ol/format/GeoJSON";
import Style from "ol/style/Style";
import Stroke from "ol/style/Stroke";
import Fill from "ol/style/Fill";
// import Select from "ol/interaction/Select"
import {bbox} from 'ol/loadingstrategy';
export default {
  data() {
    return {
    
    };
  },
  mounted() {
    this.initMap()
  },
  methods: {
    initMap(){
      var extent = [11285.07103919199,20056.574012374178,61290.31172946711,33996.47243386325];
      var wfsVectorSource = new VectorSource({
        url: 'http://localhost:8082/geoserver/workhome/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=workhome%3A28f&outputFormat=application%2Fjson',
        format: new GeoJSON(),
        features: Array.Feature,
        strategy: bbox
      })

      var styles = new Style({
        stroke: new Stroke({
            // color: 'blue',
            color: 'rgba(30,144,255)',
            width: 3
        }),
        fill: new Fill({
            color: 'rgba(0, 0, 255, 0.1)'
        })
      })

      var wfsVectorLayer = new VectorLayer({
        style: styles,
        source: wfsVectorSource,
        visible:true,
      })
      
      this.map = new Map({
        target: "map",
        layers: [
          wfsVectorLayer
        ],
        view: new View({
            center: [31955.4551374715, 28165.253430237015],
            projection: 'EPSG:3857',
            zoom: 14
        }),
      });
      this.map.getView().fit(extent, this.map.getSize());
      this.map.getView().setZoom(14);
};
</script>

<style>
#map{height:100%;}
/*隐藏ol的一些自带元素*/
.ol-attribution,.ol-zoom { display: none;}

.mapStyle {
  background-color: black
}
</style>

效果:
在这里插入图片描述
ok,打完收工

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值