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是否显示比例尺,

 

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值