天天看点

vue 封装openlayers的Map组件

一、组件的封装

(1)依赖文件的导入

// 映入css文件、
import 'ol/ol.css';
import '../utils/openlayers/css/OverviewMap.css';
import Map from 'ol/Map.js';

import View from 'ol/View.js';
import TileLayer from 'ol/layer/Tile.js';
import XYZ from 'ol/source/XYZ';
// 比例尺
import ScaleLine from 'ol/control/ScaleLine';
// 地图服务
import {tiandituVetorMap,tiandituImgMap} from "../utils/openlayers/js/mapServer"
           

(2) 地图容器的创建

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

(3) 实现地图的加载

export default {
    props:["zoom","center","isShowBaseMap","isScaleLine"],
    data(){
        return{
            map:null
        }
    },
    mounted(){
        this.initMap();
    },
    methods:{
        initMap(){
            this.map = new Map({
                target: 'map',
                view: new View({
                    projection: 'EPSG:4326',
                    center:this.center || [0, 0],
                    zoom: this.zoom || 2
                }),
            });
            if(this.isShowBaseMap)
                tiandituVetorMap(this.map)
                // this.showTianDiTu();
            if(this.isScaleLine)
                this.showScaleLine();
        },    
        // 显示比例尺
        showScaleLine:function(){
             //实例化比例尺控件(ScaleLine)
            var scaleLineControl = new ScaleLine({
                //设置比例尺单位,degrees、imperial、us、nautical、metric(度量单位)
                units: "metric"
            });
            this.map.addControl(scaleLineControl)
        }
    }
}
</script>
           

二、组件全部代码

<template>
    <div>
         <div id="map" class="map"></div>
    </div>
</template>
<script>
// 映入css文件、
import 'ol/ol.css';
import '../utils/openlayers/css/OverviewMap.css';
import Map from 'ol/Map.js';

import View from 'ol/View.js';
import TileLayer from 'ol/layer/Tile.js';
import XYZ from 'ol/source/XYZ';
// 比例尺
import ScaleLine from 'ol/control/ScaleLine';
// 地图服务
import {tiandituVetorMap,tiandituImgMap} from "../utils/openlayers/js/mapServer"
export default {
    props:["zoom","center","isShowBaseMap","isScaleLine"],
    data(){
        return{
            map:null
        }
    },
    mounted(){
        this.initMap();
    },
    methods:{
        initMap(){
            this.map = new Map({
                target: 'map',
                view: new View({
                    projection: 'EPSG:4326',
                    center:this.center || [0, 0],
                    zoom: this.zoom || 2
                }),
            });
            //加载天地图,
            if(this.isShowBaseMap)
                tiandituVetorMap(this.map)
                // this.showTianDiTu();
            if(this.isScaleLine)
                this.showScaleLine();
        },    
        // 显示比例尺
        showScaleLine:function(){
             //实例化比例尺控件(ScaleLine)
            var scaleLineControl = new ScaleLine({
                //设置比例尺单位,degrees、imperial、us、nautical、metric(度量单位)
                units: "metric"
            });
            this.map.addControl(scaleLineControl)
        }
    }
}
</script>
<style>
    .map{
        width: 100%;
        height:70%;
    }
   
</style>


           

三、组件的使用

(1) 引入组件

import Map from './components/Map'
           

(2) 申明组件

components: {
    Map
  },
           

(3) 组件的页面显示

<Map class="map" ref="map" zoom='6' :center="center" :isShowBaseMap="true" :isScaleLine="true" />
           

(4) 说明: zoom是初始化缩放级别,center是地图中心,isShowBaseMap是否显示地图(默认是天地图),isScaleLine是否显示比例尺,

GIS