Vue结合Openlayers示例

1、简单示例

先看下以引入< JavaScript >的方式的使用方法

1.1创建一个mapView组件

mapView.js

export default {
  template: '<div id="map"></div>',
  data() {
    return {
      map: null
    }
  },
  mounted() {
    this.initMap()
  },
  methods: {
    initMap(){
      let baseLayer = new ol.layer.Tile({
        source: new ol.source.XYZ({
          url: 'https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}'
        })
      })
  
      let view = new ol.View({
        projection: 'EPSG:4326',
        zoom: 4,
        center: [114, 32]
      })
  
      this.map = new ol.Map({
        target: 'map',
        layers: [baseLayer],
        view: this.view
      })
    }
  }
}

1.2 创建一个Vue实例,并注册mapView组件

import mapView from './components/mapView.js'

const app = new Vue({
  components: {mapView}
}).$mount('#app')

1.3在页面中应用

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Vue结合openlayers</title>
  <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
  <script src="https://openlayers.org/en/v4.6.5/build/ol.js" type="text/javascript"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <style>
    head, body, .mapview{
      height: 100%;
      width: 100%;
    }
  </style>
</head>
<body>
  <div id="app">
    <map-view class="mapview"></map-view>
  </div>
  <script src="./main.js" type="module"></script>
</body>
</html>

2、地图配置

通常,为了灵活使用地图起见,我们把地图的一些常用设置,提取出来单独配置。比如:地图的view(视图)配置,假设我们需要在地图初始化时设置地图的中心点在北京,我们写了一个配置文件
mapConfig.js

const mapConfig = {
  viewOption: {
    projection: 'EPSG:4326',
    zoom: 12,
    center: [116.405,39.9228], //北京中心点坐标
    maxZoom: 16,
    minZoom: 4
  }
}

export default mapConfig

而后,修改mapView,让它的**initMap()**方法可以接收view参数配置

export default {
  template: '<div id="map"></div>',
  data() {
    return {
      map: null
    }
  },
  // mounted() {
  //   this.initMap()
  // },
  methods: {
    initMap(options){
      let viewOptions = options.view ? options.view : {
        projection: 'EPSG:4326',
        zoom: 15,
        center: [108.944, 34.3615]
      }

      let baseLayer = new ol.layer.Tile({
        source: new ol.source.XYZ({
          url: 'https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}'
        })
      })
  
      let view = new ol.View(viewOptions)
  
      this.map = new ol.Map({
        target: 'map',
        layers: [baseLayer],
        view: this.view
      })
    }
  }
}

接着,修改main.js,把参数传递进去,
main.js

import mapView from './components/mapView.js'
import mapConfig from './config/mapConfig.js'

const app = new Vue({
  components: {mapView},
  mounted() {
    this.$refs['mapview'].initMap({
      view: mapConfig.viewOption
    })
  },
}).$mount('#app')

同时,html也做微调,给mapview标签加上ref属性,这样就可以通过Vue.$refs访问到这个组建

<map-view class="mapview" ref="mapview"></map-view>

假设后续要把地图中心点调整到其它地方,只要修改mapConfig.js中的配置就可以了。

3、npm 构建

如果时通过npm 的方式来在Vue框架中使用Openlayers,方法与此大同小异。
假设你项目中已经安装了Vue,接下来

3.1 安装openlayers

npm install ol

3.2 在mapView组建中导入相关模块

mapView.js

import {Map, View} from 'ol';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ'

export default {
  template: '<div id="map"></div>',
  data() {
    return {
      map: null
    }
  },
  // mounted() {
  //   this.initMap()
  // },
  methods: {
    initMap(options){
      let viewOptions = options.view ? options.view : {
        projection: 'EPSG:4326',
        zoom: 15,
        center: [108.944, 34.3615]
      }

      let baseLayer = new TileLayer({
        source: new XYZ({
          url: 'https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}'
        })
      })
  
      let view = new View(viewOptions)
  
      this.map = new Map({
        target: 'map',
        layers: [baseLayer],
        view: view
      })
    }
  }
}

示例下载

您好!对于VueOpenLayers结合使用返回围栏(fence)的问题,您可以按照以下步骤进行操作: 1. 首先,确保您已经安装了VueOpenLayers的依赖包。您可以使用以下命令进行安装: ``` npm install vue ol ``` 2. 在您的Vue组件中,引入OpenLayers的相关模块和样式,例如: ```javascript import 'ol/ol.css'; import { Map, View } from 'ol'; import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer'; import { OSM, Vector as VectorSource } from 'ol/source'; import { Draw, Modify } from 'ol/interaction'; import Feature from 'ol/Feature'; import GeometryType from 'ol/geom/GeometryType'; ``` 3. 在组件的`mounted`生命周期钩子中,创建地图和相关图层,并添加交互操作。下面是一个示例: ```javascript mounted() { // 创建地图容器 const map = new Map({ target: 'map-container', layers: [ new TileLayer({ source: new OSM() }), new VectorLayer({ source: new VectorSource() }) ], view: new View({ center: [0, 0], zoom: 2 }) }); // 添加绘制交互操作 const draw = new Draw({ source: map.getLayers().item(1).getSource(), type: GeometryType.POLYGON }); map.addInteraction(draw); // 添加修改交互操作 const modify = new Modify({ source: map.getLayers().item(1).getSource() }); map.addInteraction(modify); // 监听绘制结束事件 draw.on('drawend', (event) => { const feature = event.feature; const geometry = feature.getGeometry(); // 这里可以处理绘制结束后的围栏数据,例如发送到后端进行保存或其他操作 console.log(geometry.getCoordinates()); }); } ``` 4. 在组件的模板中,添加一个用于显示地图的容器: ```html <template> <div id="map-container"></div> </template> ``` 通过以上步骤,您可以在Vue中使用OpenLayers创建一个地图,并使用绘制交互操作绘制围栏。当绘制结束时,可以获取到围栏的坐标信息进行后续处理。 希望对您有所帮助!如有任何问题,请随时提问。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值