(五)openlayers加圆形以及引入element-ui组件

(1)第一节我写了基于vue3的openlayers项目创建,其实还可以进行如下操作,进入一个文件夹,输入cmd,如图所示:
在这里插入图片描述
创建完成后,用vscode打开项目文件夹,新建终端输入:npm run serve,进行运行。
(2)本节主要引入element-ui组件以及基于openlayers加圆形。Element UI是一个基于Vue.js的开源UI组件库,专为开发者提供了一套优雅而灵活的UI组件。它包含了各种常用的Web组件,如按钮、表单、导航等,以及高级组件,如日期选择器、对话框等。Element UI的设计风格简洁美观,易于定制,使其成为许多Vue.js项目的首选UI框架。对于初学者来说很适合。
首先引入Element UI,进入官网找到基于Vue3版本的安装命令,在vscode终端里面输入这个命令:npm install element-plus --save
在这里插入图片描述
安装成功,package.json会显示这样:
在这里插入图片描述
然后在main.js中引入element -ui,如图所示:
在这里插入图片描述
以上我们就可以引入element-ui的组件,本次我们引入element-ui中的按钮组件。直接在官网中复制代码:
在这里插入图片描述
一定要注意对应是Vue3版本的,不要复制错了,我这直接复制链接:element-ui按钮
然后根据自己所需对代码进行改写,我这还是延续之前章节的代码,所以进行如下改写:
在这里插入图片描述
运行显示:
在这里插入图片描述
(3)基于openlayers加圆形。引入需要的依赖:
在这里插入图片描述
编写加圆形的函数:
在这里插入图片描述
整体运行显示如下:
在这里插入图片描述(4)整个代码如下:

<template>
    <!-- 模板部分,包含一个 id 为 "map" 的地图容器和按钮 -->
    <!-- <div>
        <button @click="zoomIn" class="Bigbutton">放大</button>
        <button @click="zoomOut" class="Smallbutton">缩小</button>
        <button @click="toggleLayer" class="toggle">显示/隐藏图层</button>
        <button @click="addCircle">加圆</button>
        <button @click="removeCircle">移除</button>
    </div> -->
    <div>
        <el-button @click="zoomIn" class="Bigbutton">放大</el-button>
        <el-button @click="zoomOut" class="Smallbutton">缩小</el-button>
        <el-button @click="toggleLayer" class="toggle">显示/隐藏图层</el-button>
        <el-button @click="addCircle">加圆</el-button>
        <el-button @click="removeCircle">移除</el-button>
        <div id="map" class="map"></div>
    </div>
</template>
<script>
// 导入 Vue 相关库
import { onMounted, ref } from "vue";
// 导入 OpenLayers 样式文件
import "ol/ol.css";
// 导入 OpenLayers 相关模块
import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import Zoom from 'ol/control/Zoom';
import wuhan from "@/assets/武汉市.json";
import GeoJSON from "ol/format/GeoJSON";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import { Circle } from "ol/geom";
import Feature from "ol/Feature";
import Style from "ol/style/Style";
import Fill from "ol/style/Fill";
import Stroke from "ol/style/Stroke";
export default {
    name: "BaseMap",
    setup() {
        // 使用 ref 创建响应式对象存放地图实例
        const map = ref(null); // 存放地图实例
        let circleLayer = null;
        // 初始化地图函数
        function initMap() {
            // 创建地图实例
            map.value = new Map({
                target: 'map', // 地图容器的 id
                layers: [ // 图层
                    new TileLayer({ // 使用瓦片渲染方法
                        source: new XYZ({
                            url:'http://webst0{1-4}.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}',
                            crossOrigin:"anonymous"
                        }) // 图层数据源
                    }),
                    // 加载矢量图层
                    new VectorLayer({
                        source:new VectorSource({
                            features:new GeoJSON().readFeatures(wuhan, {featureProjection:"EPSG:4326"}),
                            wrapX:true
                        })
                    })
                ],
                view: new View({
                    projection: "EPSG:4326", // 地图视图
                    center: [114.4, 30.5], // 地图中心点坐标为武汉的经纬度
                    zoom: 12,
                    // extent: [114, 30, 116, 30.6] // 地图初始缩放级别
                }),
                controls: [
                    new Zoom() // 添加缩放控件
                ]
            });
        }
        // 放大地图函数
        function zoomIn() {
            let view = map.value.getView();
            let zoom = view.getZoom();
            view.setZoom(zoom + 1);
        }
        // 缩小地图函数
        function zoomOut() {
            let view = map.value.getView();
            let zoom = view.getZoom();
            view.setZoom(zoom - 1);
        }
        // 显示/隐藏图层函数
        function toggleLayer() {
            let layers = map.value.getLayers(); // 获取图层组
            let layer = layers.getArray()[0]; // 获取第一个图层
            let visible = layer.getVisible(); // 获取图层当前显示状态
            layer.setVisible(!visible); // 设置图层显示状态
        }
        function addCircle() {
            const circleFeature = new Feature({
                geometry: new Circle([114.4, 30.6],0.1)
            });
            const source = new VectorSource({
                features:[circleFeature]
            });
            circleLayer = new VectorLayer({
                source:source,
                style:new Style({
                    fill:new Fill({
                        color:'rgba(0,0,255,0.1)'
                    }),
                    stroke:new Stroke({
                        color:"#ce602c",
                        width:1
                    })
                })
            });
            map.value.addLayer(circleLayer)
        }
        function removeCircle() {
  	            // 移除圆形图层
             if (circleLayer) {
                map.value.removeLayer(circleLayer);
                circleLayer = null; // 将 circleLayer 置为 null,以便下次添加圆形图层时重新创建
    } 
        }
        // 在组件挂载完成后执行初始化地图函数
        onMounted(() => { 
            initMap();
        });
        // 返回响应式数据和方法
        return {
            map,
            toggleLayer,
            zoomIn,
            zoomOut,
            addCircle,
            removeCircle
        };
    }
};
</script>
<style>
/* 可选的样式 */
.map {
    width: 2000px;
    height: 2000px;
    border: 1px solid #eee;
}
.Bigbutton {
    background-color: red;
}
.Smallbutton {
    background-color: greenyellow;
}
.toggle {
    background-color:skyblue;
}
</style>


<think>嗯,用户问的是如何在vue3-openlayers中使用ol-attribution-control组件。首先,我需要回忆一下vue3-openlayers的基本用法,以及这个组件的具体功能。 ol-attribution-control是OpenLayers中的一个控件,用于显示地图的版权信息。在Vue3项目中,通过vue3-openlayers库来使用这个控件,应该需要引入对应的组件标签,然后进行配置。记得用户之前提到的引用[1]和[2]里提到了安装依赖的方法,比如使用ol@6.15.1和ol-ext,所以可能需要确认vue3-openlayers是否兼容这些版本。 首先,用户需要安装vue3-openlayers,可能需要使用npm或yarn安装。然后,在组件引入ol-attribution-control,并作为子组件放在ol-map里。需要设置collapsible属性来控制是否可以折叠,可能还有其他的属性比如collapsed,是否默认折叠。 另外,用户可能需要自定义样式,比如调整控件的位置,或者修改字体颜色。这时候需要写一些CSS样式,通过class来覆盖默认样式。同时,如果不想显示默认的控件,可能需要设置地图的controls选项,排除原来的attribution控件。 还要注意,是否需要在初始化地图时禁用默认的attribution控件,然后手动添自定义的。或者,vue3-openlayers是否提供了直接的方式来配置这些控件。可能还需要参考vue3-openlayers的官方文档,确认ol-attribution-control的可用属性和事件。 用户可能遇到的问题包括控件不显示,样式冲突,或者版本兼容性问题。需要提醒用户检查安装的版本是否正确,以及是否正确引入组件。同时,如果用户需要动态控制版权信息的内容,可能需要通过数据绑定或者方法来实现,比如根据不同的地图源切换attribution内容。 最后,生成相关问题的时候,要考虑到用户可能下一步想知道如何自定义其他控件,或者如何处理多个控件的布局,以及如何与Vue3的其他功能结合使用,比如状态管理或者动态组件。</think>在`vue3-openlayers`中使用`ol-attribution-control`组件需遵循以下步骤: --- ### 一、基础配置 1. **安装依赖** ```bash npm install vue3-openlayers ol@6.15.1 # 或 yarn add vue3-openlayers ol@6.15.1 ``` 2. **组件注册** 在Vue组件中导入控件: ```vue <script setup> import { OlMap, OlAttributionControl } from 'vue3-openlayers'; </script> ``` --- ### 二、基本用法 在`<ol-map>`标签内直接嵌套控件,通过属性配置行为: ```vue <template> <ol-map :controls="[]"> <!-- 禁用默认控件 --> <ol-attribution-control :collapsible="true" :collapsed="false" class="custom-attribution" /> <!-- 其他地图图层 --> </ol-map> </template> ``` - `:controls="[]"`用于禁用OpenLayers默认的版权控件[^1] - `collapsible`控制是否允许折叠 - `class`用于自定义CSS样式 --- ### 三、样式自定义 通过CSS调整位置和外观: ```css .custom-attribution { right: 10px; bottom: 5px; background: rgba(255,255,255,0.8); color: #333; } ``` --- ### 四、动态内容绑定 可通过响应式数据控制显示内容: ```vue <script setup> import { ref } from 'vue'; const attributionText = ref('© 2023 Map Data'); </script> <template> <ol-attribution-control :html="attributionText" /> </template> ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值