前端学习openLayers配合vue3(加载矢量图标)
今天我们来进行矢量图标的加载
关键代码
有一个比较注意的点就是,图片路径必须引入不能直接写路径,我找半天也没发现问题所在
let anchorLayer=new VectorLayer({
source: new VectorSource(),
});
let anchorFeatures=new Feature({
geometry: new Point(center),
})
//重点,需要引入才行,不能在src上写路径
const iconSrc = require('./image.png');
anchorFeatures.setStyle(new Style({
image: new Icon({
src: iconSrc,
}),
}))
anchorLayer.getSource().addFeature(anchorFeatures);
map.addLayer(anchorLayer)
效果图(图片是有点大了,不过原理是这个原理,真正开发中的话让ui把图做小点)

完整代码
<script setup>
import { onMounted, reactive, ref } from "vue";
import { Feature, Map, View } from "ol";
import TileLayer from "ol/layer/Tile";
import { OSM, XYZ } from "ol/source";
import { fromLonLat } from "ol/proj";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import GeoJSON from "ol/format/GeoJSON";
import Style from "ol/style/Style";
import Fill from "ol/style/Fill";
import Stroke from "ol/style/Stroke";
import Icon from "ol/style/Icon";
import { Point } from "ol/geom"; defineProps({
msg: String,
});
let map = reactive({});
let view = reactive({});
// let count=ref(0)
// let center=[114.305469, 30.592876];
let center=reactive([114.305469, 30.592876]);
onMounted(() => {
initMap();
});
let initMap = () => {
(view = new View({
center ,
zoom: 5,
projection: "EPSG:4326",
})),
(map = new Map({
target: "map", //挂载视图的容器
layers: [
//瓦片图层source第三方,或者自带的,地图的底层
new TileLayer({
// source: new OSM(),//内置的国外地址,需要代理
source: new XYZ({
url: "http://wprd0{1-4}.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=7",
}), //国内第三方数据源
}),
// 矢量图层
new VectorLayer({
source: new VectorSource({
url: "https://geo.datav.aliyun.com/areas_v3/bound/100000_full.json",
format: new GeoJSON(),
}),
//填充颜色
style: new Style({
fill: new Fill({
color: "rgba(255, 0, 0, 0.5)",
}),
stroke: new Stroke({
color: "black",
width: 1,
})
}),
}),
], //视图
view: view,
})); let anchorLayer=new VectorLayer({
source: new VectorSource(),
});
let anchorFeatures=new Feature({
geometry: new Point(center),
})
const iconSrc = require('./image.png');
anchorFeatures.setStyle(new Style({
image: new Icon({
src: iconSrc,
}),
}))
anchorLayer.getSource().addFeature(anchorFeatures);
map.addLayer(anchorLayer) };
let move = () => {
// 设置北京的经纬度
const beijing = [116.46, 39.92];
const view = map.getView();
view.animate({
center: beijing,
zoom: 10,
projection: "EPSG:4356",
});
};
</script> <template>
<div id="map">
<div class="btns">
</div>
</div>
</template> <style scoped>
.btns {
display: flex;
position: fixed;
left: 20px;
bottom: 20px; z-index: 999;
}
.btns div {
width: 100px;
height: 40px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
.read-the-docs {
color: #888;
}
#map {
margin: 0;
width: 100vw;
height: 100vh;
}
</style>
前端学习openLayers配合vue3(加载矢量图标)的更多相关文章
- 《前端之路》之 前端图片 类型 & 优化 & 预加载 & 懒加载 & 骨架屏
目录 09: 前端图片 类型 & 优化 & 预加载 & 懒加载 & 骨架屏 09: 前端图片 类型 & 优化 & 预加载 & 懒加载 & ...
- Jquery前端分页插件pagination同步加载和异步加载
上一篇文章介绍了Jquery前端分页插件pagination的基本使用方法和使用案例,大致原理就是一次性加载所有的数据再分页.https://www.jianshu.com/p/a1b8b1db025 ...
- Java虚拟机JVM学习02 类的加载概述
Java虚拟机JVM学习02 类的加载概述 类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个java.lang.Class对 ...
- ios网络学习------4 UIWebView的加载本地数据的三种方式
ios网络学习------4 UIWebView的加载本地数据的三种方式 分类: IOS2014-06-27 12:56 959人阅读 评论(0) 收藏 举报 UIWebView是IOS内置的浏览器, ...
- thinkphp学习笔记9—自动加载
原文:thinkphp学习笔记9-自动加载 1.命名空间自动加载 在3.2版本中不需要手动加载类库文件,可以很方便的完成自动加载. 系统可以根据类的命名空间自动定位到类库文件,例如定义了一个类Org\ ...
- 微信小程序:添加全局的正在加载中图标效果
在发送请求的时候,显示一个正在加载中的小图标.在加载下一页的时候也显示正在加载中.同时数据请求回来了,把加载中进行关闭. 开发----API-----界面 在哪里添加这两段代码会比较方便呢?一个项目有 ...
- 前端性能优化--图片懒加载(lazyload image)
话说前头: 上次写了一篇webpack的学习心得,webpack能做到提升前端的性能,其模块打包最终生成一个或少量的文件能够减少对服务端的请求.除此之外,本次的图片懒加载(当然不仅限于图片,还可以有视 ...
- webpack学习笔记--按需加载
为什么需要按需加载 随着互联网的发展,一个网页需要承载的功能越来越多. 对于采用单页应用作为前端架构的网站来说,会面临着一个网页需要加载的代码量很大的问题,因为许多功能都集中的做到了一个 HTML 里 ...
- Web前端性能优化——提高页面加载速度
前言: 在同样的网络环境下,两个同样能满足你的需求的网站,一个“Duang”的一下就加载出来了,一个纠结了半天才出来,你会选择哪个?研究表明:用户最满意的打开网页时间是2-5秒,如果等待超过10秒, ...
- (转)Openlayers 2.X加载高德地图
http://blog.csdn.net/gisshixisheng/article/details/44853881 概述: 前面的有篇文章介绍了Openlayers 2.X下加载天地图,本节介绍O ...
随机推荐
- 在react中使用阿里图标库
参考教程:https://blog.csdn.net/qq_41977441/article/details/110352463 阿里图标库:https://www.iconfont.cn/ 成果展示 ...
- Git命令缩写
g = 'git' ga = 'git add' gaa='git add --all' gapa='git add --patch' gb='git branch' gba='git branch ...
- Clickhouse SQL语法
Insert 基本与标准 SQL(MySQL)基本一致 (1)标准 insert into [table_name] values(-),(-.) (2)从表到表的插入 insert into [ta ...
- Ansible自动化管理集群,Playbook语法
Ansible配置文件存在优先级的问题 ANSIBLE_CONFIG ansible.cfg 项目目录 .ansible.cfg 当前用户的家目录 /etc/ansible/ansible.cfg A ...
- 强行修改 User-Agent, 访问对应的端
location /{ proxy_pass http://localhost:18080; proxy_set_header User-Agent "Mozilla/5.0 (Window ...
- Vulhub Apache Httpd漏洞复现
目录 前言 多后缀解析漏洞 换行解析漏洞(CVE-2017-15715) 2.4.49 路径穿越漏洞(CVE-2021-41773) 2.4.50 路径穿越漏洞(CVE-2021-42013) SSR ...
- js之模块导入与导出:export、export default、module.exports、exports
前两者export.export default可为一组,是es6的规范,和import匹配,import是es6中的语法标准:后两者module.exports.exports可为一组,是commo ...
- golang之泛型
Go 1.18版本增加了对泛型的支持,泛型也是自 Go 语言开源以来所做的最大改变. 泛型允许程序员在强类型程序设计语言中编写代码时使用一些以后才指定的类型,在实例化时作为参数指明这些类型.ーー换句话 ...
- highcharts中的折线图
折现图表的样式如下所示: 整体的一个设置代码如下: that.options = { title: { text: null }, subtitle: { text: null }, yAxis: { ...
- 小白PDF阅读器开发-页面元素分割
以前用手机看PDF格式的电子书时,总感觉非常别扭,PDF格式的电子书在手机上缩放严重,字体太小,想看清楚得来回放大拖动,看书的兴致就在来回缩放拖动间被消耗没了!每次用手机看PDF电子书时就想着得做款能 ...