码迷,mamicode.com
首页 > 其他好文 > 详细

Vue集成openlayers

时间:2019-02-25 13:41:50      阅读:1553      评论:0      收藏:0      [点我收藏+]

标签:文件夹   var   target   head   exp   home   color   server   from   

1.安装ol库

使用cnpm i ol -s命令安装

2.建一个olMap.vue文件夹

技术图片
<template>
    <div class=‘olMap‘>
        <h2>{{this.name}}</h2>
        <div class=‘map‘  ref=‘rootmap‘></div>
    </div>
</template>

<script>
import ‘ol/ol.css‘
import { Map, View } from ‘ol‘
import mapconfig from ‘../../mapconfig‘

export default {
    name: ‘HomeMap‘,
    data () {
        return {
            name: ‘地图展示‘,
            map: null
        }
    },
    mounted () {
        var mapcontainer = this.$refs.rootmap
        this.map = new Map({
          target: mapcontainer,
          layers: mapconfig.streetmap(),
          view: new View({
            projection: ‘EPSG:4326‘,    
            center: [mapconfig.x, mapconfig.y], 
            zoom: mapconfig.zoom
          })
        })
    }
}

</script>

<style lang=‘stylus‘>
    .olMap
        width: 100%
        .map
            height: 100%
            .ol-attribution
                display: none
            .ol-zoom 
                display: none
</style>    
View Code

3.在首页home.vue中使用该olMap组件

技术图片
<template>
    <div>
      <home-header></home-header>
         <home-swiper></home-swiper>
    <home-content></home-content>
         <home-map></home-map>
     </div>
</template>

<script>
import HomeHeader from ‘./components/Header‘
import HomeSwiper from ‘./components/Swiper‘
import HomeContent from ‘./components/Content‘
import HomeMap from ‘./components/olMap‘

export default {
  name: ‘Home‘,
  components: {
    HomeHeader,
    HomeSwiper,
    HomeContent,
    HomeMap
  }
}
</script>

<style>
</style>
View Code

4.使用mapconfig.js文件

技术图片
import TileLayer from ‘ol/layer/Tile‘
import TileArcGISRest from ‘ol/source/TileArcGISRest‘
import OSM from ‘ol/source/OSM‘
import XYZ from ‘ol/source/XYZ‘

let maptype = 1         
//0表示部署的离线瓦片地图,1表示OSM,2表示使用Arcgis在线午夜蓝地图服务
var streetmap=function(){
    var maplayer = null
    switch(maptype){
        case 0:
            maplayer=new TileLayer({
                source: new XYZ({
                    url:‘http://127.0.0.1:7080/streetmap/shenzhen/{z}/{x}/{y}.jpg‘
                })
            })
        break
        case 1:
            maplayer=new TileLayer({
                source: new OSM()
            })
        break
        case 2:
            maplayer=new TileLayer({
                source:new TileArcGISRest({
                    url:‘https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer‘
                })
            })
        break
    }
    return [maplayer]
}

var mapconfig = {
    x: 114.064839,     
    y: 22.548857,
    zoom: 15,          
    streetmap: streetmap
}

export default mapconfig
View Code

5.效果如下

技术图片

 源码地址:https://gitee.com/newGlobe/vueLearn/tree/master/src/pages/

Vue集成openlayers

标签:文件夹   var   target   head   exp   home   color   server   from   

原文地址:https://www.cnblogs.com/gisblog/p/10430258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!