vue项目中openlayers画行政区划(区域范围)

vue项目中openlayers画行政区划(区域范围)

一级目录

原理

在地图上画需要的范围,实际上就是在地图上打上一圈点,然后依次将这些点用线连接,就形成了范围

引用相应的ol模块

import VectorLayer from 'ol/layer/Vector'
import VectorSource from 'ol/source/Vector'
import { Map, View, Feature } from 'ol'
import { Style, Icon, Stroke } from 'ol/style'
import { Point, LineString, Polygon } from 'ol/geom'

获取范围点

这里我将点放在json文件中,然后通过axios读取
json文件截图:
json文件截图

      axios.get('static/常德市.json').then((res) => {
        let arr = res.data.coordinates
        let polygonFeature = new Feature({
          type: 'polygon',
          geometry: new Polygon(arr[0])
        })
        polygonFeature.setStyle(new Style({
          stroke: new Stroke({
            width: 2,

            color: [255, 255, 0, 0.8]
          }),
          fill: new Fill({
            color: [248, 172, 166, 0.2]
          })
          // text: new Text({
          //   text: '这是区域'
          // })
        }))
        let polygonLayer = new VectorLayer({
          source: new VectorSource({
            features: [polygonFeature]
          })
        })
        this.gmap.addLayer(polygonLayer)
      })
      axios.get('static/怀化市沅陵县.json').then((res) => {
        let arr = res.data.coordinates
        let polygonFeature = new Feature({
          type: 'polygon',
          geometry: new Polygon(arr[0])
        })
        polygonFeature.setStyle(new Style({
          stroke: new Stroke({
            width: 2,
            color: [255, 255, 0, 0.8]
          }),
          fill: new Fill({
            color: [248, 172, 166, 0.2]
          })
          // text: new Text({
          //   text: '这是区域'
          // })
        }))
        let polygonLayer = new VectorLayer({
          source: new VectorSource({
            features: [polygonFeature]
          })
        })
        this.gmap.addLayer(polygonLayer)
      })

在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue项目静态加载OpenLayers可以通过以下步骤实现: 1. 首先,确保你的Vue项目已经安装了OpenLayers的依赖包。你可以使用npm或者yarn来安装OpenLayers,具体的安装命令可以参考OpenLayers的官方文档。 2. 在Vue项目的入口文件(通常是main.js),引入OpenLayers的库文件。你可以使用import语句来引入OpenLayers的库文件,例如: ```javascript import 'ol/ol.css'; import { Map, View } from 'ol'; ``` 这样就可以在Vue项目使用OpenLayers的相关功能了。 3. 在Vue组件,你可以使用OpenLayers的API来创建地图、添加图层、设置视图等。你可以根据你的需求来使用OpenLayers的各种功能,例如: ```javascript // 创建地图容器 const map = new Map({ target: 'map', layers: \[ // 添加图层 new TileLayer({ source: new OSM() }) \], view: new View({ center: \[0, 0\], zoom: 2 }) }); ``` 这样就可以在Vue项目静态加载OpenLayers,并创建一个简单的地图。 需要注意的是,以上只是一个简单的示例,你可以根据你的具体需求来使用OpenLayers的更多功能和API。同时,你也可以参考引用\[1\]和引用\[2\]提供的示例代码来学习更多关于在Vue项目使用OpenLayers的技巧和方法。 #### 引用[.reference_title] - *1* [112:vue+openlayers 加载静态图片,变地图一部分( 示例代码 )](https://blog.csdn.net/cuclife/article/details/126607890)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [219:vue+openlayers 加载gif文件,采用CSS设置gif背景的方式](https://blog.csdn.net/cuclife/article/details/129497398)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值