今天我们来进行矢量图标的加载
关键代码
有一个比较注意的点就是,图片路径必须引入不能直接写路径,我找半天也没发现问题所在
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>
标签:ol,let,center,vue3,source,openLayers,import,new,图标 From: https://www.cnblogs.com/wcq520/p/18657309